lundi 29 juin 2015

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

Aucun commentaire:

Enregistrer un commentaire