扫描器类hasNextLine无限循环 [英] Scanner Class hasNextLine infinite loop

查看:127
本文介绍了扫描器类hasNextLine无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  import为什么这段代码会进入一个无限循环当我尝试给它一个基本的文本文件? java.io.File的; 
import java.io.FileWriter;
import java.io.IOException;
import java.util。*;
public class TestFile
{

public static void main(String args [])throws IOException
{
//读入输入文件
文件输入=新文件(args [0]);
扫描仪freader =新扫描仪(输入); $(b)b(b)(freader.hasNextLine()){
System.out.println(hi);
}

freader.close();



$ b code
$ b

打印行只是继续前进。

也不改变扫描仪的状态。如果它是真的,并且没有其他方法的扫描器被调用,它将永远是真实的。


why does this piece of code go into an infinite loop when I try to give it a basic text file?

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class TestFile
{

    public static void main(String args[]) throws IOException
    {
            // Read in input file                                                                                                                                            
            File input = new File(args[0]);
            Scanner freader = new Scanner(input);
            while (freader.hasNextLine()) {
                System.out.println("hi");
            }

            freader.close();

    }

}

The print line just keeps going.

解决方案

Because hasNexLine() does neither get the line nor change the state of the scanner. if it's true once, and no other methods of the scanner are called, it'll always be true.

这篇关于扫描器类hasNextLine无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆