简体   繁体   English

扫描仪未从文件读取内容

[英]Scanner not reading content from file

I am trying to write the number 0 to a file called amountSessions the first time it is created as you can see in the if statement below the comment "On the first run" which works so far, but later on in the program (at the end of the writeToFile method) below the comment "Increment number each run" I want to increment the 0 I discussed earlier by 1 each time the program runs. 我正在尝试将数字0首次创建时写入到名为amountSessions的文件中,正如您在if语句下方的“第一次运行”注释中所看到的那样,到目前为止,它仍然有效,但后来在程序中(在我想在程序每次运行时将前面讨论的0递增1。 Unfortunetly when I do compile and run the program I am given the following error. 不幸的是,当我编译并运行程序时,出现以下错误。 Why is this? 为什么是这样? Thank you very much everybody! 非常感谢大家!

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;
    }
}

Change 更改

} else if(!amountSessions.exists())

To

}
if(!amountSessions.exists())

Otherwise only the first file will be created if neither exist. 否则,如果第一个文件都不存在,则只会创建第一个文件。

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

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