Java 了解 Scanner 的 nextLine()、next() 和 nextInt() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32798803/
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
Understanding Scanner's nextLine(), next(), and nextInt() methods
提问by bangbang
I am trying to understand how these three methods work. Here's how I understood them:
我试图了解这三种方法是如何工作的。以下是我对它们的理解:
nextLine()
reads the remainder of the current line even if it is empty.nextInt()
reads an integer but does not read the escape sequence "\n".next()
reads the current line but does not read the "\n".
nextLine()
读取当前行的剩余部分,即使它是空的。nextInt()
读取一个整数但不读取转义序列“\n”。next()
读取当前行但不读取“\n”。
Suppose I have the following code:
假设我有以下代码:
import java.util.Scanner;
public class Welcome2
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Next enter two words:");
int n;
String s1, s2;
n = keyboard.nextInt();
s1 = keyboard.next();
s2 = keyboard.nextLine();
System.out.println(" n is " + n + " s1 is " + s1 + " s2 is " + s2);
}
}
If my input is :
如果我的输入是:
2
Hi
Hello
Then I get the following output on screen:
然后我在屏幕上得到以下输出:
n is 2
s1 is hi
s2 is
Why would s1
have a value of "HI"?
为什么s1
会有值“HI”?
Does this mean that the method next()
reads the next line even though the escape character for the first line has not been read by nextInt()
?
这是否意味着next()
即使第一行的转义字符尚未被 读取,该方法也会读取下一行nextInt()
?
采纳答案by Stephen C
nextLine() reads the remainder of the current line even if it is empty.
nextLine() 读取当前行的剩余部分,即使它是空的。
Correct.
正确的。
nextInt() reads an integer but does not read the escape sequence "\n".
nextInt() 读取一个整数但不读取转义序列“\n”。
Correct1.
正确1。
next() reads the current line but does not read the "\n".
next() 读取当前行但不读取“\n”。
Incorrect.In fact, the next()
method reads the next complete token. That may or may not be the rest of the current line. And it may, or may not consume an end-of line, depending on where the end-of-line is. The precise behavior is described by the javadoc, and you probably need to read it carefully for yourself in order that you can fully understand the nuances.
不正确。实际上,该next()
方法读取下一个完整的标记。这可能是也可能不是当前行的其余部分。它可能会,也可能不会消耗行尾,具体取决于行尾的位置。确切的行为由 javadoc 描述,您可能需要自己仔细阅读,以便您能够完全理解细微差别。
So, in your example:
所以,在你的例子中:
The nextInt() call consumes the
2
character and leaves the position at theNL
.The next() call skips the
NL
, consumesH
andi
, and leaves the cursor at the second NL.The nextLine() call consumes the rest of the 2nd line; i.e. the
NL
.
nextInt() 调用消耗
2
字符并将位置留在NL
.next() 调用跳过
NL
、消耗H
和i
,并将光标留在第二个 NL。nextLine() 调用消耗了第二行的其余部分;即
NL
。
1 ... except that your terminology is wrong. When the data is being read, it is not an escape sequence. It is an end-of-line sequence that could consist of a CR, a NL or a CR NL depending on the platform. The escape sequences you are talking about are in Java source code, in string and character literals. They may >>represent<< a CR or NL or ... other characters.
1 ...除了您的术语是错误的。读取数据时,它不是转义序列。它是一个行尾序列,可以由 CR、NL 或 CR NL 组成,具体取决于平台。您正在谈论的转义序列在 Java 源代码中,在字符串和字符文字中。它们可能>>代表<< CR 或NL 或...其他字符。
回答by Animesh Kumar Paul
If your input is 2 hello hi
如果您的输入是 2 hello hi
nextInt() - just read the next token as a int (Here - it is 2
) otherwise it give error
nextInt() - 只需将下一个标记作为 int 读取(这里 - 它是2
),否则会出错
next() - Just read the next token, (here - it is hello
)
next() - 只需读取下一个标记,(这里 - 它是hello
)
nextline() - It read a line until it gets newline (here - after read previous 2 hello
input by nextInt, next; nextline read hi
only because after that it finds a newline )
nextline() - 它读取一行直到它获得换行符(这里 - 在2 hello
通过 nextInt, next读取上一个输入之后;nextline 只读,hi
因为之后它找到了一个换行符)
if input is
如果输入是
2
Hi
Hello
nextInt, next is same for above discussion .
nextInt, next 与上述讨论相同。
In nextline(), it finds newline after completing the input Hi
read by next(). So, nextline() stops to read input for getting the newline character.
在 nextline() 中,它在完成Hi
next() 读取的输入后找到换行符。因此,nextline() 停止读取输入以获取换行符。
回答by JumpMan
Does this mean that the method next() reads the next line even though the escape character for the first line has not been read by nextInt()?
这是否意味着即使 nextInt() 没有读取第一行的转义字符,next() 方法也会读取下一行?
NO.
不。
next()
cannot read next line, it only reads something before space.When
keyboard.nextLine()
executes, it consumes the "end of line" still in the buffer from the first input.when you enter a number then press Enter,
keyboard.nextInt()
consumes only the number, not the "end of line".
next()
无法读取下一行,它只读取空间之前的内容。当
keyboard.nextLine()
执行时,它会消耗来自第一输入缓冲器中的“行结束”静止。当您输入一个数字然后按 Enter 时,
keyboard.nextInt()
只会消耗该数字,而不是“行尾”。
Using next() will only return what comes before a space. nextLine() automatically moves the scanner down after returning the current line.
使用 next() 只会返回空格之前的内容。nextLine() 在返回当前行后自动向下移动扫描仪。