Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java hasNextLine(),而循环未退出_Java_While Loop_Java.util.scanner - Fatal编程技术网

Java hasNextLine(),而循环未退出

Java hasNextLine(),而循环未退出,java,while-loop,java.util.scanner,Java,While Loop,Java.util.scanner,我知道我需要使用hasNextLine()来推进该行-但是当我引用hasNextLine()时,我不确定为什么它仍然没有退出while循环: 我在这里使用扫描仪(sc)读取输入文件 while(sc.hasNextLine()){ //read post specifications from line String postspecs = sc.nextLine();

我知道我需要使用hasNextLine()来推进该行-但是当我引用hasNextLine()时,我不确定为什么它仍然没有退出while循环:

我在这里使用扫描仪(sc)读取输入文件

while(sc.hasNextLine()){                            
//read post specifications from line
                            String postspecs = sc.nextLine();

                            //split line
                            String[] postspecs_s = postspecs.split(", ");


                            //create post
                            reddit.findUser(stripExtension(argfile.getName())).
                            addPost(postspecs_s[0], PostType.valueOf
                                    (postspecs_s[1]), postspecs_s[2]);

                            System.out.println("created");
                        }

仅当达到EOF(文件结尾)字符时,'hasNextLine'才会返回false。停止读取的一种简单方法是等待从System.in读取的哨兵值

所以你会有一个

String s = sc.nextLine();
while(!s.equals(SENTINEL))
    //proceed and dont forget to re-read.

我认为ctrl+z会将EOF发送到Windows上的system.in,在iPod atm上,因此无法进行测试。

它在做什么?它是逐行读取txt文件,并将该文件的元素存储到字符串数组中。然后我使用字符串数组的元素创建一个对象,如前几行所示。最后,我打印创建了(只是为了知道它经过while循环多少次),我知道它应该做什么。既然你说它不起作用,我想它不会起作用。它做什么?它没有退出while循环-这意味着它将继续打印创建的文件。如果打印出
postspecs
它会打印什么?