使用扫描仪获取用户输入 [英] Getting User input with Scanner
问题描述
我正在尝试让扫描仪在循环中接收输入。一旦用户想要完成,他就可以退出这个循环。我尝试了很多不同的方法,但总有一些问题。这是代码:
I am trying to have a scanner take input in a loop. Once the user wants to finish he can exit this loop. I have tried many different ways to do it but there is always some problem. This is the code:
private void inputEntries() {
Scanner sc = new Scanner(System.in);
System.out.println("Continue?[Y/N]");
while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here
System.out.println("Enter first name");
String name = sc.nextLine();
System.out.println("Enter surname");
String surname = sc.nextLine();
System.out.println("Enter number");
int number = sc.nextInt();
Student student = new Student(name, surname, number);
students.add(student);
try {
addToFile(student);
} catch (Exception ex) {
Logger.getLogger(TextReader.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Continue?[Y/N]");
}
}
上面代码的问题,也发生在我尝试过的不同方法是,当用户键入 Y 时, Scanner
将跳过第一个名字输入,并跳转到姓氏。如果用户键入 N ,则循环正确停止。有人可以解释这种情况发生的原因,以及如何克服使用扫描仪
类?
The problem with the code above, which also happens on different methods I tried, is that when the user types Y, the Scanner
will skip the first input for first name,and jump to the surname. If the user types N the loop stops correctly. Someone can explain the reason this happens, and how to overcome using Scanner
class?
ps:做类似 while(sc.nextLine()。equals(Y))
,将导致循环在首次运行循环后从用户输入之前终止。
p.s: Doing something like while(sc.nextLine().equals("Y"))
, will cause the loop to terminate before getting input from user after first run of the loop.
推荐答案
这是因为你正在使用 扫描仪#next
方法。如果你查看该方法的文档,它将返回下一个标记读取。
This is because you are using Scanner#next
method. And if you look at the documentation of that method, it returns the next token read.
因此,当您使用 next
方法读取用户输入时,它不会读取新行
结尾。然后在中的
循环。因此,您的 nextLine()
读取,而 firstName
包含换行符
,无需您知道。
So, when you read user input using next
method, it does not read the newline
at the end. Which is then read by the nextLine()
inside the while
loop. And thus, your firstName
contains a newline
without you knowing.
所以,你应该在你的while中使用 nextLine()
而不是 next()
。
So, you should use nextLine()
in your while rather than next()
.
类似于 nextInt
方法。它也不会读取换行符。因此,您可以使用
并将其转换为 readLine
读取 int
使用 Integer.parseInt
。如果输入值无法转换为 int
,它可以抛出 NumberFormatException
。因此,您需要相应地处理它。
Similar is the case with nextInt
method. It also does not read the newline. So, you can read
using readLine
and convert it to int
using Integer.parseInt
. It can throw NumberFormatException
if input value cannot be converted to int
. So you need to handle it accordingly.
您可以尝试以下代码: -
You can try the below code: -
Scanner sc = new Scanner(System.in);
System.out.println("Continue?[Y/N]");
while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here
System.out.println("Enter first name");
String name = sc.nextLine();
System.out.println("Enter surname");
String surname = sc.nextLine();
System.out.println("Enter number");
int number = 0;
try {
number = Integer.parseInt(sc.nextLine());
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
System.out.println("Continue?[Y/N]");
}
但是,请注意,如果输入的值无法传递给 Integer.parseInt
您将获得异常,并且将跳过该输入。对于这种情况,您需要使用处理它,而
循环。
But, note one thing, if you enter a value that cannot be passed to Integer.parseInt
you will get an exception, and that input will be skipped. For that case, you need to handle it by using while
loop.
或者,如果你不喜欢我不想做异常处理: -
你可以添加一个空sc.nextLine()
在 sc.nextInt()
之后,将消耗剩下的换行符
,如下所示: -
You can add an empty sc.nextLine()
after sc.nextInt()
, that will consume the newline
left over, like this: -
// Left over part of your while loop
String surname = sc.nextLine();
System.out.println("Enter number");
int number = sc.nextInt();
sc.nextLine(); // To consume the left over newline;
System.out.println("Continue?[Y/N]");
这篇关于使用扫描仪获取用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!