[英]Java Input Needed Twice - I parsed an integer so I could use scanner.next();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// printing menu
System.out.println(menu);
boolean quit = false;
int selection;
do {
// next user input (of integer type) will be stored in the variable selection
selection = Integer.parseInt(scanner.next());
switch (selection) {
case 1:
System.out.println("Please enter a new record as John Michael West Doe, 574 Pole ave, St. Peter, MO, 63303, 3142752000");
scanner.useDelimiter(",");
Person[] people = new Person[0];
while (scanner.hasNext()) {
String fullName = scanner.next();
String street = scanner.next();
String city = scanner.next();
String state = scanner.next();
String zipC = scanner.next();
String phoneN = scanner.next();
System.out.println(fullName);
}
break;
case 2:
// method to delete record - remove
break;
case 3:
// search by telephone number
break;
case 4:
// find record by first name
break;
case 5:
// find record by last name
break;
case 6:
// update a record
break;
case 7:
quit = true;
break;
default:
System.out.println("Invalid");
}
} while (selection != 7);
}
Hi, I'm currently working on case 1 - my problem is that I have to input the code twice for it to accept it and print out the fullName string..嗨,我目前正在处理案例 1 - 我的问题是我必须输入两次代码才能接受它并打印出 fullName 字符串..
Even at the beginning, when I make a selection and press 1 and enter, I have to type another key before the 'please enter a new record.....' line pops up.即使在开始时,当我进行选择并按 1 并输入时,我必须在弹出“请输入新记录.....”行之前键入另一个键。
I had just learned about what nextInt does and how it leaves a new line, so I parsed it in order to use scanner.next();我刚刚了解了 nextInt 的作用以及它如何换行,因此我对其进行了解析以使用 scanr.next();
I'm just trying to see where the hang up is.我只是想看看挂断的地方。
Thank you.谢谢你。
There is zero difference between scanner.nextInt()
and Integer.parseInt(scanner.next())
, except for the kinds of errors that are thrown if the next token in the input stream isn't an integer. scanner.nextInt()
和Integer.parseInt(scanner.next())
之间的差异为零,除了在输入流中的下一个标记不是整数时抛出的错误类型。 You must have misunderstood a few things.你一定误解了一些事情。
I pasted your code into a file, compiled it, ran it, and cannot reproduce your observed behaviour of 'I have to press enter twice'.我将您的代码粘贴到一个文件中,对其进行编译、运行,但无法重现您观察到的“我必须按两次 Enter”的行为。 I just press enter once, and
Please enter...
shows.我只按回车一次,然后
Please enter...
显示。
There is nothing wrong with your code.您的代码没有任何问题。 At least, nothing that would explain why you need to double-enter here.
至少,没有什么可以解释为什么你需要在这里重复输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.