英文:
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("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) {
//there is a check to make sure the contact doesn'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("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}";
}
}
And the while
loop:
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;
}
}
答案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("Name: ");
String name = scanner.nextLine();
//scanner.nextLine(); <-- here is the reason
System.out.print("Number: ");
String number = scanner.nextLine();
phone.addContact(name, number);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论