Java Scanner 类读取字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1466418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Scanner class reading strings
提问by marcoamorales
I got the following code:
我得到以下代码:
int nnames;
String names[];
System.out.print("How many names are you going to save: ");
Scanner in = new Scanner(System.in);
nnames = in.nextInt();
names = new String[nnames];
for (int i = 0; i < names.length; i++){
System.out.print("Type a name: ");
names[i] = in.nextLine();
}
And the output for that code is the following:
该代码的输出如下:
How many names are you going to save:3
Type a name: Type a name: John Doe
Type a name: John Lennon
Notice how it skipped the first name entry?? It skipped it and went straight for the second name entry. I have tried looking what causes this but I don't seem to be able to nail it. I hope someone can help me. Thanks
注意它是如何跳过名字条目的??它跳过它并直接进入第二个名称条目。我试过寻找导致这种情况的原因,但我似乎无法解决它。我希望有一个人可以帮助我。谢谢
采纳答案by Joshua
The reason for the error is that the nextInt only pulls the integer, not the newline. If you add a in.nextLine() before your for loop, it will eat the empty new line and allow you to enter 3 names.
错误的原因是 nextInt 只拉整数,而不是换行符。如果在 for 循环之前添加 in.nextLine() ,它将占用空的新行并允许您输入 3 个名称。
int nnames;
String names[];
System.out.print("How many names are you going to save: ");
Scanner in = new Scanner(System.in);
nnames = in.nextInt();
names = new String[nnames];
in.nextLine();
for (int i = 0; i < names.length; i++){
System.out.print("Type a name: ");
names[i] = in.nextLine();
}
or just read the line and parse the value as an Integer.
或者只是读取该行并将值解析为整数。
int nnames;
String names[];
System.out.print("How many names are you going to save: ");
Scanner in = new Scanner(System.in);
nnames = Integer.parseInt(in.nextLine().trim());
names = new String[nnames];
for (int i = 0; i < names.length; i++){
System.out.print("Type a name: ");
names[i] = in.nextLine();
}
回答by Nettogrof
It's because the in.nextInt() doesn't change line. So you first "enter" (after you press 3 ) cause the endOfLine read by your in.nextLine() in your loop.
这是因为 in.nextInt() 不会改变行。所以你首先“输入”(在你按下 3 之后)导致你的 in.nextLine() 在你的循环中读取 endOfLine 。
Here a small change that you can do:
这是您可以做的一个小改动:
int nnames;
String names[];
System.out.print("How many names are you going to save: ");
Scanner in = new Scanner(System.in);
nnames = Integer.parseInt(in.nextLine());
names = new String[nnames];
for (int i = 0; i < names.length; i++){
System.out.print("Type a name: ");
names[i] = in.nextLine();
}
回答by wangzhengyi
This because in.nextInt() only receive a int number, doesn't receive a new line. So you input 3 and press "Enter", the end of line is read by in.nextline().
这是因为 in.nextInt() 只接收一个 int 数字,不接收新行。所以你输入 3 并按“Enter”,行尾由 in.nextline() 读取。
Here is my code:
这是我的代码:
int nnames;
String names[];
System.out.print("How many names are you going to save: ");
Scanner in = new Scanner(System.in);
nnames = in.nextInt();
in.nextLine();
names = new String[nnames];
for (int i = 0; i < names.length; i++){
System.out.print("Type a name: ");
names[i] = in.nextLine();
}
回答by Angelin Nadar
You could have simply replaced
你可以简单地更换
names[i] = in.nextLine();
with names[i] = in.next();
names[i] = in.nextLine();
和 names[i] = in.next();
Using next() will only return what comes before a space. nextLine() automatically moves the scanner down after returning the current line.
使用 next() 只会返回空格之前的内容。nextLine() 在返回当前行后自动向下移动扫描仪。
回答by Ankur Nirmalkar
use sc.nextLine(); two time so that we can read the last line of string
使用 sc.nextLine(); 两次以便我们可以读取字符串的最后一行
sc.nextLine() sc.nextLine()
sc.nextLine() sc.nextLine()