Java 扫描仪未从文件中读取内容

Java 扫描仪未从文件中读取内容,java,file,filewriter,Java,File,Filewriter,我试图在第一次创建名为amountSessions的文件时将数字0写入该文件,正如您在注释“第一次运行时”下面的if语句中看到的那样,该注释到目前为止仍然有效,但稍后在程序中(writeToFile方法末尾)注释“每次运行时递增数字”下面的程序中可以看到每次程序运行时,我想将前面讨论的0增加1。不幸的是,当我编译并运行程序时,我得到了以下错误。为什么会这样?非常感谢大家 Exception in thread "main" java.util.NoSuchElementException at

我试图在第一次创建名为amountSessions的文件时将数字0写入该文件,正如您在注释“第一次运行时”下面的if语句中看到的那样,该注释到目前为止仍然有效,但稍后在程序中(writeToFile方法末尾)注释“每次运行时递增数字”下面的程序中可以看到每次程序运行时,我想将前面讨论的0增加1。不幸的是,当我编译并运行程序时,我得到了以下错误。为什么会这样?非常感谢大家

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at math_program.fileWriting.getInt(fileWriting.java:62)
at math_program.fileWriting.writeToFile(fileWriting.java:41)

//Test

package testing;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class Kappa
{
    String numberFilePath = "/Users/john/Desktop/numProblems.txt";
    String sessionFilePath = "/Users/john/Desktop/amountSessions.txt";
    File testFile = new File(numberFilePath);
    File amountSessions = new File(sessionFilePath); 

    public void writeToFile() throws IOException
    {

        //On the first run
        if(!testFile.exists())
        {
            testFile.createNewFile();
        } else if(!amountSessions.exists())
        {
            amountSessions.createNewFile();
            FileWriter sessionWriter = new FileWriter(amountSessions);
            sessionWriter.write("0");
            sessionWriter.close();
        }

        FileWriter durationWriter = new FileWriter(testFile);
        FileWriter sessionWriter = new FileWriter(amountSessions);

        //Write Duration
        String ans = prompt();
        durationWriter.write(ans);
        durationWriter.close();

        //Increment number each run
        sessionWriter.write(getInt());
        sessionWriter.close();

        System.exit(0); 
    }

    public void grab() throws FileNotFoundException
    {
        Scanner numProblemsReader = new Scanner(new File("/Users/john/Desktop/numProblems.txt"));
        Scanner numSessionReader = new Scanner(new File("/Users/john/Desktop/amountSessions.txt"));
        int number = numProblemsReader.nextInt();
        int numSessions = (numSessionReader.nextInt() + 1);

        System.out.println("The number read from the file is: " + number);
        File replaceFile = new File("/Users/john/Desktop/session" + numSessions + ".txt");
        testFile.renameTo(replaceFile);
    }

    public String getInt() throws FileNotFoundException
    {
        Scanner numSessionReader = new Scanner(new File("/Users/john/Desktop/amountSessions.txt"));
        int numSessions = ((numSessionReader.nextInt()) + 1);
        String newNum = Integer.toString(numSessions);
        return newNum;
    }

    public String prompt()
    {
        String ans = JOptionPane.showInputDialog ("Enter the new amount of problems per training session (with number in minutes):");                        

        while(!ans.matches("[0-9]+"))
        {
            ans = JOptionPane.showInputDialog ("Please re-enter the new amount of problems per training session (with number in minutes):" ); 
        }    

        return ans;
    }
}
改变


否则,如果两个文件都不存在,则只创建第一个文件。

请不要打开读/写同一文件两次,因为您试图同时从/向同一文件读/写。不要。先阅读所有内容,然后再写作。我该如何解决这个问题?我应该调用一个包含所有文件写入的方法吗?我该如何修复我的代码?它修复了没有任何一个文件但希望它们都存在的情况。不,它实际上没有修复任何东西,因为即使使用else,如果它仍然创建了两个文件。我很想知道它是如何做到的,因为这些条件块中只有一个可以执行。你确定没有其他东西在创建一个或两个文件吗?我不知道如何创建,请不要误会我我不是在试图破坏你或其他任何东西,但我发誓,当我编译代码时,这就是它给我的。难道不值得尝试弄清楚为什么这两个文件都存在吗?如果有其他内容正在创建该文件,则可能是将int以外的内容写入该文件(例如,nothing或String)。别担心,我没有生气,我只是建议也修复这个bug。
} else if(!amountSessions.exists())
}
if(!amountSessions.exists())