为什么我不能在函数内部保存来自Scanner的用户输入?

huangapple go评论116阅读模式
英文:

Why can't I save user input from Scanner inside a function?

问题

我有一个Phone类,只有一个字段:Contact类的实例的ArrayList。Contact类只有两个字段:名称和号码。
当你运行程序时,

  • 它会显示编号的指令
  • 你输入一个数字
  • 它使用switch语句调用执行相应操作的函数,如下所示:
switch (num) {
    case 0:
        printInstructions();
        break;
    case 1:
        addContact();
        break;
}

switch语句位于一个while循环中。
我已经将提示用户输入信息(例如名称)的代码放在了函数内部:

public static void addContact() {
    System.out.print("Name: ");
    String name = scanner.nextLine();
    scanner.nextLine();
    
    System.out.print("Number: ");
    String number = scanner.nextLine();
    phone.addContact(name, number);
}

phone.addContact()

public void addContact(String name, String number) {
    // 确保联系人不存在的检查
    contacts.add(new Contact(name, number));
}

我的问题是:我无法将我输入的名称保存下来。无论我输入什么,name的值始终是空字符串。
更奇怪的是,number保存正确。为什么会发生这种情况呢?

编辑:而且,如果我去掉循环、switch语句和函数,并在main()中直接运行代码,它就能正常工作。

(我还需要分享其他代码吗?)

有人要求我包含Contact类,所以——

public class Contact {
    private String name;
    private String number;
    
    public Contact(String name, String number) {
        this.name = name;
        this.number = number;
        System.out.println("Contact created with name " + name + " and number " + number);
    }
    
    public Contact(String name) {
        this(name, null);
    }
    
    public Contact() {
        this("J. Doe", null);
        System.out.println("ERROR: blank contact somehow generated");
    }
    
    public String getName() {
        return name;
    }
    
    public String getNumber() {
        return number;
    }
    
    @Override
    public String toString() {
        return "Contact { \n" +
                "    name = \"" + name + "\",\n" +
                "    number = \"" + number + "\"" +
                "\n}";
    }
}

以及while循环:

while (true) {
    System.out.print("Enter an instruction number: ");
    num = phoneScnr.nextInt();
    switch (num) {
        case 0:
            printInstructions();
            break;
        case 1:
            addContact();
            break;
        case 2:
            editContact();
            break;
        case 3:
            removeContact();
            break;
        case 4:
            findContact();
            break;
        case 5:
            listContacts();
            break;
        case -1:
            quit = true;
            break;
        default:
            System.out.println("Sorry, that character is not recognized.");
            break;
    }
}
英文:

I have a Phone class that has only one field: an ArrayList of instances of the Contact class. The Contact class has only two fields: name and number.
<br> When you run the program,

  • It displays numbered instructions
  • You enter a number
  • It calls the function the performs the corresponding action using a switch statement, like so:

    switch (num) {
        case 0:
            printInstructions();
            break;
        case 1:
            addContact();
            break;
    }

The switch statement is in a while loop.
I've put the code to prompt the user for information (like a name) inside the functions:


    public static void addContact() {
        System.out.print(&quot;Name: &quot;);
        String name = scanner.nextLine();
        scanner.nextLine();
    
        System.out.print(&quot;Number: &quot;);
        String number = scanner.nextLine();
        phone.addContact(name, number);
    }

phone.addContact():


    public void addContact(String name, String number) {
        //there is a check to make sure the contact doesn&#39;t already exist
        contacts.add(new Contact(name, number));
    }

My problem: I can't get it to save the name I enter. The value of name stays an empty string no matter what I enter.
What's stranger is that the number is saved correctly. Why is this happening?

<br>Edit: Also, It works fine if I get rid of the loop and the switch statement and the function and run the code directly in main().

(Is there any other code I should share?)

I was asked to include the Contact class, so —


    public class Contact {
        private String name;
        private String number;
    
        public Contact(String name, String number) {
            this.name = name;
            this.number = number;
            System.out.println(&quot;Contact created with name &quot; + name + &quot; and number &quot; + number);
        }
    
        public Contact(String name) {
            this(name, null);
        }
    
        public Contact() {
            this(&quot;J. Doe&quot;, null);
            System.out.println(&quot;ERROR: blank contact somehow generated&quot;);
        }
    
        public String getName() {
            return name;
        }
    
        public String getNumber() {
            return number;
        }
    
        @Override
        public String toString() {
            return &quot;Contact { \n&quot; +
                    &quot;    name = \&quot;&quot; + name + &quot;\&quot;,\n&quot; +
                    &quot;    number = \&quot;&quot; + number + &quot;\&quot;&quot; +
                    &quot;\n}&quot;;
        }
    }

And the while loop:


    while (true) {
                System.out.print(&quot;Enter an instruction number: &quot;);
                num = phoneScnr.nextInt();
                switch (num) {
                    case 0:
                        printInstructions();
                        break;
                    case 1:
                        addContact();
                        break;
                    case 2:
                        editContact();
                        break;
                    case 3:
                        removeContact();
                        break;
                    case 4:
                        findContact();
                        break;
                    case 5:
                        listContacts();
                        break;
                    case -1:
                        quit = true;
                        break;
                    default:
                        System.out.println(&quot;Sorry, that character is not recognized.&quot;);
                        break;
                }
            }

答案1

得分: 4

请不要在读取姓名后再次读取扫描仪的行:

public static void addContact() {
    System.out.print("姓名:");
    String name = scanner.nextLine();
    //scanner.nextLine(); <-- 这里是原因

    System.out.print("号码:");
    String number = scanner.nextLine();
    phone.addContact(name, number);
}
英文:

try not reading the scanner's Line again after reading the Name :

public static void addContact() {
    System.out.print(&quot;Name: &quot;);
    String name = scanner.nextLine();
    //scanner.nextLine(); &lt;-- here is the reason

    System.out.print(&quot;Number: &quot;);
    String number = scanner.nextLine();
    phone.addContact(name, number);
}

huangapple
  • 本文由 发表于 2020年8月23日 19:28:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63546395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定