2

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.

2
  • 1
    You could go the simple route and just use '\t' at the end of "Name...", "Address..." That will start all your stored Strings at the same tab index. Commented Apr 9, 2015 at 18:19
  • Java formatting string syntax - check out 1.3 width specifiers especially, might be useful Commented Apr 9, 2015 at 18:23

2 Answers 2

2

So when you say "%15s" you're saying to make the field 15 characters wide, prepended with spaces if need be. That works for the name - there's a bunch of spaces and then the name.

The address, though, is far more than 14 characters, so you get no spaces in front of it. (Note that it does not truncate at 14; 14 is simply the minimum width here.)

You haven't defined what you mean by "line up" though... do you want "1234" directly under "John"? In that case, what you would need to do is add those spaces to your printf string, and skip the width specifiers (change it to "%s" instead of "%15s").

If instead, you want the zip code "12345" under "Smith", then you'll need to vastly increase the width specifiers. The width specifier plus the prefix ("Name:" or "Address:") will need to add up to the same number. For example, use "%50s" for the name and use "%47s" for the address (since there are three more characters in "Address:" than there are in "Name:").

Sign up to request clarification or add additional context in comments.

Comments

0

Try combining the first and last name together into one single variable ( fullName ) and then using that new variable, rather than using two separate strings.

4 Comments

If It was up to me I would do that. My professor wants me to use separate variables for the last and the first name.
Your displayName() method can't start with String fullname = firstName + " " + lastName; and then printf() with fullname?
Exactly as @dcsohl said above, you can keep them as separate variables, however i dont actually believe you can pass two variables into printf's parameters. There are most likely other solutions, however i stress this one is one of the most straight forward.
This is starting to go off topic. I realize that its easier to turn the result into one variable. The outline for the program specifies that it wants me to call for two variables in the method as opposed to one variable that combines the other two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.