Hello! I need help with my Java assignment. This is what we were told to do: ...
ID: 646667 • Letter: H
Question
Hello! I need help with my Java assignment. This is what we were told to do:
......would prompt for the first name and last name and accept them as inputs and finally would output the name displaying like this:
+----------------+
| Brian De Palma |
+----------------+
The names are not to be assigned a fixed string like you did before, but must be read as input. The number of dashes will vary based on the length of the first name and last name strings read. In addition, there could be multi-word first and las names (like De Palma or de Broglie) that should be read and handled properly.
Hints:
Use the '+' operator to concatenate strings as required.
Use a string's length() method to count the number of dashes to print
Output a single dash/minus character in every iteration of a loop
Use System.out.print() to stay in the same line, System.out.println() to go to the beginning of next line
-------------------
This is what I got so far and I am lost.
import java.util.Scanner;
public class FullNamePrinter
{
public static void main(String[] args)
{
String firstName,
lastName;
//Create scanner to obtain user input
Scanner scanner1 = new Scanner( System.in );
//obtain user input
System.out.print("Enter your first name: ");
firstName = scanner1.nextLine();
System.out.print("Enter your last name: ");
lastName = scanner1.nextLine();
//output information
System.out.print("Your first name is " + firstName + " and your last name is "+ lastName);
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 6; j++)
{
System.out.print("- ");
}
System.out.println();
}
}
}
------------------------
Thanks!
Explanation / Answer
Program code:
import java.util.Scanner;
public class FullNamePrinter
{
public static void main(String[] args)
{
String firstName, lastName;
// Create scanner to obtain user input
Scanner scanner1 = new Scanner(System.in);
// obtain user input
System.out.print("Enter your first name: ");
firstName = scanner1.nextLine();
System.out.print("Enter your last name: ");
lastName = scanner1.nextLine();
// output information
System.out.print("Your first name is " + firstName + " and your last name is " + lastName);
//to concatenate the string here + is being used.
String fullName=" "+firstName+" "+lastName+" ";
System.out.println();
System.out.print(" ");
//while printing the string format here the string's.length()
//method is being used.
//logic to print the upper portion of the string
for (int i = 0; i < fullName.length(); i++)
{
//to print the format in one line print() method
//is being used
if(i==0||i==fullName.length()-1)
System.out.print("+");
else
System.out.print("-");
}
System.out.println();
//print the full Name
System.out.println("|"+fullName+"|");
System.out.print(" ");
//logic to print the lower portion of the string
for (int i = 0; i < fullName.length(); i++)
{
if(i==0||i==fullName.length()-1)
System.out.print("+");
else
System.out.print("-");
}
}
}
Sample Output:
Enter your first name: Brain
Enter your last name: De Palma
Your first name is Brain and your last name is De Palma
+--------------+
| Brain De Palma |
+--------------+
Note:
The required code is added and highlighted in bold letters.
At the output in printing the name if you don’t want to print the spaces before and after the name then just remove the (“ ”) from the fullName variable where it has initialized.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.