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

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

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

问题

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

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

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

  1. public static void addContact() {
  2. System.out.print("Name: ");
  3. String name = scanner.nextLine();
  4. scanner.nextLine();
  5. System.out.print("Number: ");
  6. String number = scanner.nextLine();
  7. phone.addContact(name, number);
  8. }

phone.addContact()

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

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

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

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

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

  1. public class Contact {
  2. private String name;
  3. private String number;
  4. public Contact(String name, String number) {
  5. this.name = name;
  6. this.number = number;
  7. System.out.println("Contact created with name " + name + " and number " + number);
  8. }
  9. public Contact(String name) {
  10. this(name, null);
  11. }
  12. public Contact() {
  13. this("J. Doe", null);
  14. System.out.println("ERROR: blank contact somehow generated");
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public String getNumber() {
  20. return number;
  21. }
  22. @Override
  23. public String toString() {
  24. return "Contact { \n" +
  25. " name = \"" + name + "\",\n" +
  26. " number = \"" + number + "\"" +
  27. "\n}";
  28. }
  29. }

以及while循环:

  1. while (true) {
  2. System.out.print("Enter an instruction number: ");
  3. num = phoneScnr.nextInt();
  4. switch (num) {
  5. case 0:
  6. printInstructions();
  7. break;
  8. case 1:
  9. addContact();
  10. break;
  11. case 2:
  12. editContact();
  13. break;
  14. case 3:
  15. removeContact();
  16. break;
  17. case 4:
  18. findContact();
  19. break;
  20. case 5:
  21. listContacts();
  22. break;
  23. case -1:
  24. quit = true;
  25. break;
  26. default:
  27. System.out.println("Sorry, that character is not recognized.");
  28. break;
  29. }
  30. }
英文:

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:
  1. switch (num) {
  2. case 0:
  3. printInstructions();
  4. break;
  5. case 1:
  6. addContact();
  7. break;
  8. }

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:

  1. public static void addContact() {
  2. System.out.print(&quot;Name: &quot;);
  3. String name = scanner.nextLine();
  4. scanner.nextLine();
  5. System.out.print(&quot;Number: &quot;);
  6. String number = scanner.nextLine();
  7. phone.addContact(name, number);
  8. }

phone.addContact():

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

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 —

  1. public class Contact {
  2. private String name;
  3. private String number;
  4. public Contact(String name, String number) {
  5. this.name = name;
  6. this.number = number;
  7. System.out.println(&quot;Contact created with name &quot; + name + &quot; and number &quot; + number);
  8. }
  9. public Contact(String name) {
  10. this(name, null);
  11. }
  12. public Contact() {
  13. this(&quot;J. Doe&quot;, null);
  14. System.out.println(&quot;ERROR: blank contact somehow generated&quot;);
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public String getNumber() {
  20. return number;
  21. }
  22. @Override
  23. public String toString() {
  24. return &quot;Contact { \n&quot; +
  25. &quot; name = \&quot;&quot; + name + &quot;\&quot;,\n&quot; +
  26. &quot; number = \&quot;&quot; + number + &quot;\&quot;&quot; +
  27. &quot;\n}&quot;;
  28. }
  29. }

And the while loop:

  1. while (true) {
  2. System.out.print(&quot;Enter an instruction number: &quot;);
  3. num = phoneScnr.nextInt();
  4. switch (num) {
  5. case 0:
  6. printInstructions();
  7. break;
  8. case 1:
  9. addContact();
  10. break;
  11. case 2:
  12. editContact();
  13. break;
  14. case 3:
  15. removeContact();
  16. break;
  17. case 4:
  18. findContact();
  19. break;
  20. case 5:
  21. listContacts();
  22. break;
  23. case -1:
  24. quit = true;
  25. break;
  26. default:
  27. System.out.println(&quot;Sorry, that character is not recognized.&quot;);
  28. break;
  29. }
  30. }

答案1

得分: 4

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

  1. public static void addContact() {
  2. System.out.print("姓名:");
  3. String name = scanner.nextLine();
  4. //scanner.nextLine(); <-- 这里是原因
  5. System.out.print("号码:");
  6. String number = scanner.nextLine();
  7. phone.addContact(name, number);
  8. }
英文:

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

  1. public static void addContact() {
  2. System.out.print(&quot;Name: &quot;);
  3. String name = scanner.nextLine();
  4. //scanner.nextLine(); &lt;-- here is the reason
  5. System.out.print(&quot;Number: &quot;);
  6. String number = scanner.nextLine();
  7. phone.addContact(name, number);
  8. }

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:

确定