java 为什么 hasNext() 是 False,而 hasNextLine() 是 True?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31993377/
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
Why is hasNext() False, but hasNextLine() is True?
提问by DRich
Question
问题
How is it that for a scanner object the hasNextLine()
method returns true while the hasNext()
method returns false?
对于扫描仪对象,hasNextLine()
方法返回 true 而hasNext()
方法返回 false 是怎么回事?
Note: Based on the input file, the hasNext()
method is returning the result as expected; the hasNextLine()
does not seem to be returning the correct result.
注意:基于输入文件,该hasNext()
方法按预期返回结果;在hasNextLine()
似乎没有被返回正确的结果。
Code
代码
Here's the code I'm running that's creating the results below:
这是我正在运行的代码,它正在创建以下结果:
public void ScannerTest(Reader fileReaderObject){
Scanner scannerObj = new Scanner(fileReaderObject);
for(int i = 1; scannerObj.hasNext(); i++){
System.out.println(i + ": " + scannerObj.next());
System.out.println("Has next line: " + scannerObj.hasNextLine());
System.out.println("Has next: " + scannerObj.hasNext());
}
System.out.println();
scannerObj.close();
}
Input File
输入文件
The following is the actual content of the file that I'm passing to this scanner:
以下是我传递给此扫描仪的文件的实际内容:
a 3 9
b 3 6
c 3 3
d 2 8
e 2 5
f 2 2
g 1 7
h 1 4
i 1 1
Result
结果
The following is the end of what's printed in the console when I run my code, and includes the portion I can't make sense of:
以下是我运行代码时在控制台中打印的内容的结尾,包括我无法理解的部分:
25: i
Has next line: true
Has next: true
26: 1
Has next line: true
Has next: true
27: 1
Has next line: true
Has next: false
采纳答案by durron597
You have a single extra newline at the end of your file.
您的文件末尾有一个额外的换行符。
hasNextLine()
checks to see if there is anotherlinePattern
in the buffer.hasNext()
checks to see if there is a parseable token in the buffer, as separated by the scanner's delimiter.
hasNextLine()
检查linePattern
缓冲区中是否还有另一个。hasNext()
检查缓冲区中是否有可解析的标记,由扫描器的分隔符分隔。
Since the scanner's delimiter is whitespace, and the linePattern
is also white space, it is possible for there to be a linePattern
in the buffer but no parseable tokens.
由于扫描器的分隔符是空格,而 thelinePattern
也是空格,因此linePattern
缓冲区中可能有 a但没有可解析的标记。
Typically, the most common way to deal with this issue by always calling nextLine()
after parsing all the tokens (e.g. numbers) in each line of your text. You need to do this when using Scanner
when reading a user's input too from System.in
. To advance the scanner past this whitespace delimiter, you must use scanner.nextLine()
to clear the line delimiter. See: Using scanner.nextLine()
通常,处理此问题的最常见方法是始终nextLine()
在解析文本每一行中的所有标记(例如数字)后调用。在Scanner
从System.in
. 要将扫描仪前进到此空白分隔符,您必须使用scanner.nextLine()
清除行分隔符。请参阅:使用scanner.nextLine()
Appendix:
附录:
LinePattern
is defined to be a Pattern
that matches this:
LinePattern
被定义为Pattern
与此匹配的a :
private static final String LINE_SEPARATOR_PATTERN =
"\r\n|[\n\r\u2028\u2029\u0085]";
private static final String LINE_PATTERN = ".*("+LINE_SEPARATOR_PATTERN+")|.+$";
The default token delimiter is this Pattern
:
默认标记分隔符是这样的Pattern
:
private static Pattern WHITESPACE_PATTERN = Pattern.compile(
"\p{javaWhitespace}+");
回答by Daniel Centore
The reason is that hasNext()
checks if there are any more non-whitespace characters available. hasNextLine()
checks to see if there is another line of text available. Your text file probably has a newline at the end of it so it has another line but no more characters that are not whitespace.
原因是hasNext()
检查是否还有可用的非空白字符。hasNextLine()
检查是否有另一行文本可用。您的文本文件的末尾可能有一个换行符,因此它有另一行,但没有更多不是空白的字符。
Many text editors automatically add a newline to the end of a file if there isn't one already.
如果没有换行符,许多文本编辑器会自动在文件末尾添加一个换行符。
In other words, your input file is not this (the numbers are line numbers):
换句话说,你的输入文件不是这个(数字是行号):
1. a 3 9
2. b 3 6
3. c 3 3
4. d 2 8
5. e 2 5
It is actually this:
其实是这样的:
1. a 3 9
2. b 3 6
3. c 3 3
4. d 2 8
5. e 2 5
6.
回答by kamwo
Short answer
简答
You have an empty line at the end of the file.
文件末尾有一个空行。
Reason for the empty line
空行的原因
If you take your content and save it for example into a txt file, some editors will add an empty new line to your file.
例如,如果您将内容保存到 txt 文件中,一些编辑器会在您的文件中添加一个空的新行。
The editors behave this way, because this is part of the POSIXstandard:
编辑器的行为是这样的,因为这是POSIX标准的一部分:
3.206 Line
A sequence of zero or more non- characters plus a terminating character.
3.206线
零个或多个非字符加上终止字符的序列。
This topic has been discussed in this thread.
此主题已在此线程中讨论过。
Java Scanner documentation
Java 扫描器文档
Here is the documentation from the Java 8 Scanner class.
这是Java 8 Scanner 类的文档。
hasNext()
Returns true if this scanner has another token in its input.
hasNext()
如果此扫描器的输入中有另一个标记,则返回 true。
hasNextLine()
Returns true if there is another line in the input of this scanner.
hasNextLine()
如果此扫描仪的输入中有另一行,则返回 true。
Reason for the Java code behavior
Java 代码行为的原因
Because of the above described facts, hasNextLine()
will return true
, but hasNext()
cannot find anything, which it can recognize as Token
and returns therefore false
.
由于上述事实,hasNextLine()
将返回true
,但hasNext()
找不到任何它可以识别Token
并因此返回的内容false
。
For additional infos see durron597post.
有关其他信息,请参阅durron597帖子。
回答by Florian Schaetz
You are consuming the value of next()
, but asking for hasNext()
and hasNextLine()
. next()
, per default, returns everything to the next whitespace()
. So you are iterating through all whitespace seperated strings, and after each of them you are asking about the nextLine()
.
您正在消耗 的值next()
,但要求hasNext()
和hasNextLine()
。next()
,默认情况下,将所有内容返回到下一个whitespace()
. 因此,您正在遍历所有以空格分隔的字符串,并且在每个字符串之后您都在询问nextLine()
.
i 1 1
-> hasNextLine()
? True. hasNext()
? Also true.
i 1 1
-> hasNextLine()
? 真的。hasNext()
? 也是真的。
1 1
-> hasNextLine()
? True. hasNext()
? Also true (still a whitespace left)
1 1
-> hasNextLine()
? 真的。hasNext()
? 也是如此(仍然留有空格)
1
-> hasNextLine()
? True (Line Seperator, probably). haxNext? False, no whitespace anymore.
1
-> hasNextLine()
? 正确(可能是行分隔符)。下一个?错误,不再有空格。
回答by Manpreet gujral
The Basic concept of hasNext() and hasNextLine() is
hasNext() 和 hasNextLine() 的基本概念是
hasNextLine:- Returns true if there is another line in the input of this scanner.This method may block while waiting for input. The scanner does not advance past any input.
hasNextLine:- 如果此扫描仪的输入中有另一行,则返回 true。此方法可能会在等待输入时阻塞。扫描仪不会通过任何输入。
Returns:true if and only if this scanner has another line of input Throws: IllegalStateException - if this scanner is closed
返回:当且仅当此扫描器有另一行输入时为真 抛出:IllegalStateException - 如果此扫描器已关闭
hasNext
有下一个
Returns true if the next complete token matches the specified pattern.
如果下一个完整标记与指定模式匹配,则返回 true。
A complete token is prefixed and postfixed by input that matches the delimiter pattern. This method may block while waiting for input. The scanner does not advance past any input.
一个完整的标记由与分隔符模式匹配的输入作为前缀和后缀。此方法可能会在等待输入时阻塞。扫描仪不会通过任何输入。
Parameters:pattern - the pattern to scan for
参数:pattern - 要扫描的模式
Returns:true if and only if this scanner has another token matching the specified pattern
返回:当且仅当此扫描器有另一个与指定模式匹配的标记时才返回true
Since your last input saying true for nextLine() because A call to scan.nextLine(); returns the next token. It's important to note that the scanner returns a space and a letter, because it's reading from the end of the last token until the beginning of the next line.
由于您的最后一个输入对 nextLine() 说 true 因为对 scan.nextLine() 的调用;返回下一个令牌。重要的是要注意扫描器返回一个空格和一个字母,因为它从最后一个标记的末尾读取到下一行的开头。