I am doing an assignment for school which requires the program to get the users personal information through various methods(last name, first name, address, etc.) and output them through the main method.
PROBLEM: The printf statement works fine when displaying the name, but when it displays the rest of the users information, it won't format properly.
import java.util.Scanner;
public class PersonalInfo {
public static String lastName, firstName, address, email, phone;
public static Scanner kb = new Scanner(System.in);
public static void main(String[] args)
{
getLastName();
getFirstName();
getAddress();
getEmail();
getPhone();
displayName();
displayAddress();
displayEmailPhone();
}
//--------GET methods--------
public static void getLastName()
{
System.out.print("What is the last name of the user? ");
lastName = kb.nextLine();
}
public static void getFirstName()
{
System.out.print("Now enter the first name: ");
firstName = kb.nextLine();
}
public static void getAddress()
{
System.out.print("Now enter the address: ");
address = kb.nextLine();
}
public static void getEmail()
{
System.out.print("Now enter the email: ");
email = kb.nextLine();
}
public static void getPhone()
{
System.out.print("Lastly, enter the phone number in the format xxx-xxx-
xxxx: ");
phone = kb.nextLine();
}
//--------DISPLAY methods--------
public static void displayName()
{
System.out.printf("\nName:%15s %s", firstName, lastName);
}
public static void displayAddress()
{
System.out.printf("\nAddress:%12s", address);
}
public static void displayEmailPhone()
{
System.out.printf("\nEmail:%14s", email);
System.out.printf("\nPhone:%14s", phone);
}
}
OUTPUT RESULTS:
Name: John Smith
Address:1234 street, city, state 12345
Email:[email protected]
Phone: 123-456-7890
What could be the problem? I want the rest of the information to line up with the name.
1.3 width specifiersespecially, might be useful