java while 中的 hasNext() 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44487686/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:11:37  来源:igfitidea点击:

What does hasNext() in a While do?

javawhile-loopjava.util.scannernext

提问by Johith

I'm new to JAVA and I'm trying to read data from a .txt file. I've implemented a Scanner object 'in' Scanner in=new Scanner("file.txt");and then down the code a while loop to traverse through the content of the file.There are multiple rows of data in the file and in each row there are three strings (security number ,First name ,Last name : 01 Thomas Anderson)

我是 JAVA 新手,正在尝试从 .txt 文件中读取数据。我已经实现了一个 Scanner 对象“in” Scanner in=new Scanner("file.txt");,然后在代码中循环遍历文件的内容。文件中有多行数据,每行有三个字符串(安全号码,名字, 姓 : 01 托马斯·安德森)

while(in.hasNext()){
    String ss = in.next();
    String FName=in.next();
    String LName=in.next();

    System.out.printf("SSN: %s, FirstName: %s, LastName: %s \n",ss,FName,LName);
    }

So,what does the hasNext() method do? Does it look for end of the row or for the last row in the file or.... ?

那么,hasNext() 方法有什么作用呢?它是查找行尾还是文件中的最后一行或...?

PLEASE elaborate on the working of the above snippet (mentioning the next() method too)

请详细说明上述代码段的工作原理(也提到 next() 方法)

:)

:)

回答by Meepers55

I do recommend looking at the Javadoc, but I'll explain it for you as simple as I can.

我确实建议您查看 Javadoc,但我会尽可能简单地为您解释。

Basically, the hasNext() method returns trueif the given document contains another string. Hence why the snippet will loop if said document hasanother string. Once there are no more Strings in the entire document, hasNext() returnsfalse.

基本上,true如果给定的文档包含另一个字符串,hasNext() 方法将返回。因此,如果所述文档具有另一个字符串,那么为什么该代码段会循环。一旦整个文档中没有更多的字符串, hasNext() 就会返回false

Here's an example of the next() method: String myString = myScanner.next();This makes myString equal to the next string in the given document.

下面是 next() 方法的示例: String myString = myScanner.next();这使 myString 等于给定文档中的下一个字符串。

回答by Lydzje

Javadoc says:

Javadoc 说:

hasNext
public boolean hasNext()

Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.

next
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
公共布尔值 h​​asNext()

如果此扫描器的输入中有另一个标记,则返回 true。此方法可能会在等待输入扫描时阻塞。扫描仪不会通过任何输入。

下一个
公共字符串 next()

从此扫描器中查找并返回下一个完整的令牌。一个完整的标记前后是与分隔符模式匹配的输入。此方法可能会在等待输入扫描时阻塞,即使先前对 hasNext() 的调用返回 true。

To read all your file rows you'll have to check if there are tokens every line. Then you call 3 times the next method since you know there will be 3 tokens per line:

要读取所有文件行,您必须检查每行是否有标记。然后你调用 3 次 next 方法,因为你知道每行会有 3 个标记:

import java.io.File;
import java.util.Scanner;

public class FLoader {

    public void load(String path) {
        Scanner in = null;

        try {
            in = new Scanner(new File(path));
            while (in.hasNext()) {  
                String ss = in.next();
                String FName = in.next();
                String LName = in.next();

                System.out.printf("SSN: %s, FirstName: %s, LastName: %s \n", ss, FName, LName);
            }

        } catch (Exception e) {
            System.err.println("Loading file failed");
            e.printStackTrace();
            System.exit(-1);
        }
    }
}