为什么我的循环不起作用?

时间:2022-07-20 06:50:55

I want to create a constructor, where I will ask the user to enter a person's name which will be stored in an ArrayList, and then ask the user to enter the phone number of that same person which will also be stored in another ArrayList. This should keep looping unless the user types in "no" which would then end the loop. However, when I run the method in a demo class, the first iteration works fine but then the second time out, it does not work as it skips the user input of the person's name and jumps directly to the input of the phone number. What exactly am I doing wrong? Thanks for the help

我想创建一个构造函数,我将要求用户输入一个将存储在ArrayList中的人名,然后要求用户输入同一个人的电话号码,该号码也将存储在另一个ArrayList中。除非用户输入“no”然后结束循环,否则这应该保持循环。但是,当我在一个演示类中运行该方法时,第一次迭代工作正常,但第二次超时,它不起作用,因为它跳过用户输入的人名并直接跳转到电话号码的输入。我究竟做错了什么?谢谢您的帮助

public PhoneBookEntry()
{
    System.out.println("Enter the following requested data.");
    System.out.println("");
    int i=0;
    while(i==0)
    {       
        System.out.println("Enter the name of the person (enter 'no' to end): ");
        input_name = kb.nextLine();
        if(!input_name.equalsIgnoreCase("no"))
        {
            name.add(input_name);
            System.out.println("Enter the phone number of that person (enter '-1' to end): ");
            input_number = kb.nextLong();
            phone_number.add(input_number);
        }
        else
        {
            name.trimToSize();
            break;
        }

        System.out.println("");
    }
}

1 个解决方案

#1


3  

Your problem is with your Scanner object. Understand that Scanner's nextLong() and similar methods such as nextInt(), nextDouble(), next(), do not handle the End Of Line (EOL) token. You must take pains to handle it yourself.

您的问题出在Scanner对象上。了解Scanner的nextLong()和类似的方法(如nextInt(),nextDouble(),next())不处理行尾(EOL)令牌。你必须自己努力处理它。

One way is to add a call to nextLine() like so:

一种方法是添加对nextLine()的调用,如下所示:

    System.out.println("Enter the name of the person (enter 'no' to end): ");
    input_name = kb.nextLine();
    if(!input_name.equalsIgnoreCase("no"))
    {
        name.add(input_name);
        System.out.println("Enter the phone number of that person (enter '-1' to end): ");
        input_number = kb.nextLong();
        phone_number.add(input_number);

to this:

    System.out.println("Enter the name of the person (enter 'no' to end): ");
    input_name = kb.nextLine();
    if(!input_name.equalsIgnoreCase("no"))
    {
        name.add(input_name);
        System.out.println("Enter the phone number of that person (enter '-1' to end): ");
        input_number = kb.nextLong();
        kb.nextLine();  // **** added to handle the EOL ****
        phone_number.add(input_number);

End yes, the comments are right -- this is a terrible constructor. Constructors are not for interacting directly with the user but for creating objects only.

结束是的,评论是对的 - 这是一个可怕的构造函数。构造函数不是直接与用户交互,而是仅用于创建对象。

#1


3  

Your problem is with your Scanner object. Understand that Scanner's nextLong() and similar methods such as nextInt(), nextDouble(), next(), do not handle the End Of Line (EOL) token. You must take pains to handle it yourself.

您的问题出在Scanner对象上。了解Scanner的nextLong()和类似的方法(如nextInt(),nextDouble(),next())不处理行尾(EOL)令牌。你必须自己努力处理它。

One way is to add a call to nextLine() like so:

一种方法是添加对nextLine()的调用,如下所示:

    System.out.println("Enter the name of the person (enter 'no' to end): ");
    input_name = kb.nextLine();
    if(!input_name.equalsIgnoreCase("no"))
    {
        name.add(input_name);
        System.out.println("Enter the phone number of that person (enter '-1' to end): ");
        input_number = kb.nextLong();
        phone_number.add(input_number);

to this:

    System.out.println("Enter the name of the person (enter 'no' to end): ");
    input_name = kb.nextLine();
    if(!input_name.equalsIgnoreCase("no"))
    {
        name.add(input_name);
        System.out.println("Enter the phone number of that person (enter '-1' to end): ");
        input_number = kb.nextLong();
        kb.nextLine();  // **** added to handle the EOL ****
        phone_number.add(input_number);

End yes, the comments are right -- this is a terrible constructor. Constructors are not for interacting directly with the user but for creating objects only.

结束是的,评论是对的 - 这是一个可怕的构造函数。构造函数不是直接与用户交互,而是仅用于创建对象。