简体   繁体   English

scan.hasNext false应为true

[英]scan.hasNext false when it should be true

So I'm a beginner at programming and I'm trying to figure out what the problem is with the 所以我是编程的初学者,我正在试图找出问题所在

following functions. 以下功能。 The problem is with the scan.hasNext() method I have two println 问题是scan.hasNext()方法我有两个println

statements ( one right after I make scan, and one on the second method ) telling me whether scan.hasNext() returns true or false . 语句( 我扫描后一个,第二个方法一个 )告诉我scan.hasNext()返回true还是false The weird thing is 奇怪的是

that the code as it is now enters the while loop which is what should happen, but when I 现在的代码进入了while循环,这是应该发生的事情,但是当我

delete one of the println statements it doesn't go in the loop and the other print 删除其中一个不在循环中的println语句,另一个打印

statement tells me scan.hasNext() is false . 声明告诉我scan.hasNext()假的

This is weird because I change nothing except for erasing a print statement. 这很奇怪,因为除了擦除print语句之外我什么也没做。 Furthermore I 而且我

found if I add one more for a total of 3 print statements it will be false again, and if I 发现如果我再添加一个共计3个打印语句,它将再次为假,如果我

add a fourth it's true again, etc. I think I just don't understand how hasNext works since I 添加第四个它是真的再次,等等。我想我只是不明白hasNext如何工作,因为我

have no idea how to follow where the "cursor" should be. 不知道如何遵循“光标”的位置。

public void createFile(String filename){    
    final File file = new File(filename);       
    try{
        scan = new Scanner(file);
        **System.out.println(scan.hasNext());**
        buff = new BufferedWriter(new FileWriter(filename), 1);         
    }
    catch(Exception e1){
        try{
            buff = new BufferedWriter(new FileWriter(filename));
            scan = new Scanner(file);
        }
        catch(Exception e2){
            System.out.println("Error trying to create file");
        }   
    }
}

public String readFile(){
    String fileContents = "";
    **System.out.println(scan.hasNext());**
    if(scan.hasNext())
        while(scan.hasNext())
            fileContents = fileContents + scan.next()+" "+scan.next()+" "+scan.next();
    return fileContents;

}

Okay so I figured it out. 好的,所以我明白了。 vishal_aim's suggestion helped me figure it out. vishal_aim的建议帮我搞清楚了。 The following line 以下行

buff = new BufferedWriter(new FileWriter(filename), 1);

was truncating my file back to zero size. 将我的文件截断为零大小。 the reason being is because I really meant to do this. 原因是因为我真的打算这样做。

buff = new BufferedWriter(new FileWriter(filename, true));

Firstly I put one because I thought java automatically made 1 true when it asked for a boolean. 首先我放一个,因为我认为当java要求布尔值时,java会自动生成1。 Secondly I was trying to use a constructor of the FileWriter class that lets me append to the end of a file. 其次,我试图使用FileWriter类的构造函数,让我追加到文件的末尾。 If you don't include the true with your filename it will make a new file truncating anything in the file you have to 0. This was just a tired mistake I had forgotten if I needed to put the boolean on BufferedWriter or FileWriter. 如果你没有在你的文件名中包含true,那么它会创建一个新文件,截断你需要的文件中的任何内容。这只是一个累了的错误,如果我需要将布尔值放在BufferedWriter或FileWriter上,我已经忘记了。 Also in case you're wondering the reason the BufferedWriter constructor wasn't giving me and error is because there is a constructor for it that takes an int, the int being the buffer size. 如果你想知道BufferedWriter构造函数没有给我的原因,错误是因为有一个构造函数,它接受一个int,int是缓冲区大小。 Thank you all for your contribution. 谢谢大家的贡献。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM