Java Eclipse:我的代码输出正在跳过一行,我不知道为什么

时间:2018-01-27 22:28:07

标签: java eclipse java.util.scanner

标题说明了一切。这是一个基本的Customer类,用户输入他们的姓名/年龄/街道地址/城市/州/邮政编码,然后程序格式化输入并将其返回给用户。当我上课时,它跳过街道地址'并直接前往城市'因此我无法让它输入我的街道地址。

我在这个帖子中看到了一个相当类似的问题:Java is skipping a line (Strings in a Scanner ??) 但是,我没有从那些帮助我解决这个问题的东西中获得任何东西。我确信它非常基本,但是我无法抓住它,今天没有太多时间来处理这个问题,所以任何提示/帮助都会受到赞赏!

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());


}
}

1 个答案:

答案 0 :(得分:1)

这一行之后:

actualCustomer.setAge(scan.nextInt());
你应该致电:

scan.nextLine();

因为在scan.nextInt()之后还有一个新的行字符需要阅读(在输入int之后按Enter确认输入,而你却忘记从扫描仪中读取它)。而不是写这两行:

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

您可能希望将其更改为:

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

它将摆脱新的线条角色。