java in.next() 在做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14659838/
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
What is in.next() doing?
提问by Abhishek kumar
I am using Scanner class to take input. I am trying to get all the words in a line using in.next(). I know it can be done using nextLine() but i want to understand how in.next() and in.hasNext() works.
我正在使用 Scanner 类来获取输入。我正在尝试使用 in.next() 获取一行中的所有单词。我知道可以使用 nextLine() 来完成,但我想了解 in.next() 和 in.hasNext() 是如何工作的。
System.out.println("What is designation");
String desg = in.next();
while(in.hasNext()){
desg+=in.next();
}
Gives out put as
给出放置为
What is designation
member technical staff\n
^Z
Hello abhishek kumarNext year you will be 22Salary is 30000.0Designation is membertechnicalstaff
But if i use
但如果我使用
System.out.println("What is designation");
String desg = in.next();
if(in.hasNext()){
desg+=in.next();
}
It gives output as
它给出的输出为
What is designation
member technical staff
Hello abhishek kumarNext year you will be 22Salary is 44254.0Designation is membertechnical
In the first case i am getting all the words but it keeps asking for next input and i have to specify end of input using CTRL+Z. But in second case i am not getting the last word(staff). Please explain.
在第一种情况下,我得到了所有单词,但它一直要求下一个输入,我必须使用 CTRL+Z 指定输入结束。但在第二种情况下,我没有得到最后一句话(工作人员)。请解释。
回答by amit
The first code reads the input in a while
loop - i.e. until it finds in.hasNext() == false
.
第一个代码在while
循环中读取输入- 即,直到找到为止in.hasNext() == false
。
The second code is using an if
condition - it reads in.next()
at most once (after the initial read).
第二个代码使用if
条件 - 它in.next()
最多读取一次(在初始读取之后)。
Thus, the second code is not "waiting for new input" because it simply asks for in.next()
only once, and not until input is exhausted, unlike the second code snap.
因此,与第二in.next()
个代码快照不同,第二个代码不是“等待新输入”,因为它只请求一次,并且直到输入用完才请求。
P.S. Note that the line String desg = in.next();
(in the first code snap) is a bad practice for two reasons:
PS 请注意,由于String desg = in.next();
两个原因,该行(在第一个代码快照中)是一种不好的做法:
- It will fail for empty input.
- It is a code duplication with the content of the while loop.
- 空输入将失败。
- 它是与 while 循环内容的代码重复。
回答by exexzian
its not about in.next()
the problem is with using if
it gets executed only once
and about next()
it only returns the next token
它不是关于in.next()
问题是使用if
它只执行一次并且next()
它只返回下一个令牌
so in 2nd case:(if
one)
when you enter member technical staffat this part : String desg = in.next();
only member
is assigned to desg
and after it enters if
it checks for the next token (which is technical) and concatenates it with the previous string desg+=in.next()
so now desg
becomes membertechnical
所以在第二种情况下:(if
一)当您
在这部分输入成员技术人员时:String desg = in.next();
仅member
分配给desg
并在输入后if
检查下一个令牌(技术性)并将其与前一个字符串连接,desg+=in.next()
所以现在desg
变成membertechnical
回答by kosa
If you want to learn more details about a class in API, javadoc is best source. Here is javadoc for Scanner.
如果您想了解有关 API 类的更多详细信息,javadoc 是最好的来源。这是Scanner 的 javadoc。
public String next()
Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.
从此扫描器中查找并返回下一个完整的令牌。一个完整的标记前后是与分隔符模式匹配的输入。此方法可能会在等待输入扫描时阻塞,即使先前对 hasNext() 的调用返回 true。