0

The title says it all. This is a basic Customer class that the user inputs their name/age/street address/city/state/zip code and then the program formats the input and returns it to the user. When I run this class it skips over the 'Street Address' and goes straight to 'City' and thus I can't get it to let me input my street address.

I've looked at a fairly similar issue in this thread here: Java is skipping a line (Strings in a Scanner ??) However I haven't derived anything from that that has helped me solve this issue. I'm sure its extremely basic but I'm just unable to catch it and don't have much time to work on this today, so any tips/help are appreciated!

public class Customer {
String name;
String streetAddress;
String city;
String state;
String zip;
int age;

//default constructor
public Customer() {
    name = "Unknown";
    streetAddress = "Unknown";
    city = "Unknown";
    state = "Unknown";
    zip = "Unknown";
    age = 0;
}

//constructor to accept values for the above attributes
public Customer(String n, String sAdd, String c, String st8, String z, int a) {
    name = n;
    streetAddress = sAdd;
    city = c;
    state = st8;
    zip = z;
    age = a;
}

//getters and setters
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getStreetAddress() {
    return streetAddress;
}
public void setStreetAddress(String streetAddress) {
    this.streetAddress = streetAddress;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getState() {
    return state;
}
public void setState(String state) {
    this.state = state;
}
public String getZip() {
    return zip;
}
public void setZip(String zip) {
    this.zip = zip;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

public String displayAddress() { //returns a string with the complete formatted address
    String showAddress;
    showAddress = ("\nStreet Address: " + streetAddress + "\nCity: " + city + "\nState: " + state + "\nZip Code: " + zip);
    return showAddress;
}

public String displayAddressLabel() { //returns a string with the customers name/age
    String nameAgeAddress;
    nameAgeAddress = ("Name: " + name + "\nAge: " + age);
    return nameAgeAddress;
}



//main method
public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    //creating an object of the Customer class
    Customer actualCustomer = new Customer();

    //getting info for displayAddressLabel() and displayAddress
    System.out.println("Enter your name: ");
    actualCustomer.setName(scan.nextLine());

    System.out.println("Enter your age: ");
    actualCustomer.setAge(scan.nextInt());





    //issue is here
    System.out.println("Enter your street address: ");
    actualCustomer.setStreetAddress(scan.nextLine());





    System.out.println("Enter the city you live in: ");
    actualCustomer.setCity(scan.nextLine());

    System.out.println("Enter the state you live in: ");
    actualCustomer.setState(scan.nextLine());

    System.out.println("Enter your zip code: ");
    actualCustomer.setZip(scan.nextLine());

    System.out.println(actualCustomer.displayAddressLabel());
    System.out.println(actualCustomer.displayAddress());


}
}
Ryan Horner
  • 123
  • 1
  • 1
  • 7
  • 1
    `actualCustomer.setAge(scan.nextInt());` *leaves* a trailing new line. You must consume it, or the next call to `nextLine()` will return an empty `String`. Just put `scan.nextLine();` immediately after `setAge`. Or don't call `nextInt()` (just use `nextLine()` and parse it yourself). – Elliott Frisch Jan 27 '18 at 22:30

1 Answers1

1

After this line:

actualCustomer.setAge(scan.nextInt());

you should call:

scan.nextLine();

because after scan.nextInt() there is a new line character left to be read (after inputting int you press Enter to confirm your input and you're missing to read it from your Scanner). Instead of writing these two lines:

actualCustomer.setAge(scan.nextInt());
scan.nextLine();

You might want to change it to:

actualCustomer.setAge(Integer.parseInt(scan.nextLine()));

It will get rid of new line character.

Przemysław Moskal
  • 3,321
  • 2
  • 8
  • 20