ArrayList用户输入/输出查询

时间:2014-11-08 00:12:48

标签: java

我希望得到一些帮助或指导。我对Java很新,我刚刚开始编写(尝试)ArrayList类&测试人员。我遇到了以下示例的问题..程序运行但是当我想添加另一个人的详细信息时,我得到了第一个名字&姓名请求在同一行,无法弄清楚我的错误。非常感谢任何建议。

    ArrayList <Person> details = new ArrayList<Person>();
    String fName, lName;
    int age;
    char choice = 'Y';
    do {
        System.out.print("Enter First Name: ");
        fName = keyIn.nextLine();

        System.out.print("Enter Last Name: ");
        lName = keyIn.nextLine();

        System.out.print("Enter Age: ");
        age = keyIn.nextInt();

        details.add (new Person (fName, lName, age));

        System.out.print("Add Another Person? Y/N: ");
        choice = keyIn.next().charAt(0);        
    }
    while(choice =='Y' | choice == 'y');
        for(Person p : details) {
            System.out.println(p);
        }

    }
}

2 个答案:

答案 0 :(得分:1)

当您致电age = keyIn.nextInt();时,它会消耗int,但会留下尾随的新行。所以,这个

choice = keyIn.next().charAt(0); // <-- returns immediately with '\n'.

添加

keyIn.nextLine(); // <-- consume the \n
choice = keyIn.next().charAt(0); // <-- get the next input

此外,您在while测试中缺少管道符号。

while (choice == 'Y' || choice == 'y');

答案 1 :(得分:0)

首先,这是我们如何修复您的代码的示例。我在这里所做的主要更改是使用System.out.println()静态方法,而不是不会生成“新行”的System.out.print()方法。这是一个示例方法:

public static void main( String[] args ) throws IOException{


    ArrayList<Person> myPeople = new ArrayList<Person>();//Create a person Array.
    Scanner scan = new Scanner(System.in);//Create your scanner object here.
    String keepAsking = "Y";
    do {
        System.out.println("Enter your first name: ");
        String fName = scan.next();
        System.out.println("Enter your last name: ");
        String lName = scan.next();
        System.out.println("Enter your age: ");
        int age = scan.nextInt();
        Person thisPerson = new Person(fName, lName, age);
        myPeople.add(thisPerson);
        for(Person p: myPeople) {
            System.out.println(p.getInfo());
            System.out.println();
        }
        System.out.println("Do you want to let someone else enter their info (y/n)?");
        keepAsking = scan.next();

    }while(keepAsking.equalsIgnoreCase("Y"));

    System.out.println("Thank you! Goodbye!");


}

在这种情况下,您需要使用“静态” System.out.println()方法,否则您将不会获得新行。您只是在使用System.out.print(),它不会为您开始新行,并且会弄乱您的输入。进行一些细微的改动可以在这种情况下帮助您修复代码。这是您的新输出:

Enter your first name: 
Michael
Enter your last name: 
Jordan
Enter your age: 
55
First name: Michael
Last name: Jordan
Age: 55

Do you want to let someone else enter their info (y/n)?
y
Enter your first name: 
Kobe
Enter your last name: 
Bryant
Enter your age: 
41
First name: Michael
Last name: Jordan
Age: 55

First name: Kobe
Last name: Bryant
Age: 41

Do you want to let someone else enter their info (y/n)?
y
Enter your first name: 
Tom
Enter your last name: 
Brady
Enter your age: 
41
First name: Michael
Last name: Jordan
Age: 55

First name: Kobe
Last name: Bryant
Age: 41

First name: Tom
Last name: Brady
Age: 41

Do you want to let someone else enter their info (y/n)?
n
Thank you! Goodbye!

如您所见,现在您可以为输入内容添加新的“新行”。另一个提示是,您不必每次都打印“ System.out.println()”,而是可以从技术上使用静态导入,而只需导入输出即可缩短代码:

import static java.lang.System.out;

现在您可以简单地执行以下操作:

out.println()

不必每次都键入“系统”。