Input Mismatch Exception?

699 views Asked by At

I'm still fairly new to java. I created this program for class and it's giving me an error that I have never gotten before. If someone could help that would be great. Thank you!

import java.util.Scanner;
import java.io.*;

public class grades {

    public static void main(String[] args) throws IOException {
        // Define file names 
      final String INPUT_FILE  = "gradesinput.txt";
        final String OUTPUT_FILE = "gradesoutput.txt";


      // define variables
      int grade;
      String name = null, filename;
      double value = 0;
      String msg;

        // Access the input/output files
      File inputDataFile = new File(INPUT_FILE);
        Scanner inputFile  = new Scanner(inputDataFile);
        FileWriter outputDataFile = new FileWriter(OUTPUT_FILE);
        PrintWriter outputFile = new PrintWriter(outputDataFile);
      System.out.println("Reading  file " + INPUT_FILE + "\r\n" +
                           "Creating file " + OUTPUT_FILE);

      // Read all of the values from the file 
      while (inputFile.hasNext()) {
       grade = inputFile.nextInt(); 
       name = inputFile.nextLine(); 
       name = name.trim();  

     } // End while

      if(value >= 90)      
          {          
          msg = "OUTSTANDING";
          }
          else if (value >= 70)
          {
          msg = "Satisfactory";
          }


          if(value >= 90){      
               msg = "OUTSTANDING";
     }else{
     if(value >= 70){
                    msg = "Satisfactory";                                   

     }else
                    msg = "FAILING";
              }

          outputFile.println(value + " " + name + " " + msg);
          outputFile.println("processed names");
          outputFile.println("between 70 and 89 inclusive");
          outputFile.close();

       } // End outputResults
} // End class  

I'm getting this error:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    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 grades.main(grades.java:37)
1

There are 1 answers

0
Dawnkeeper On

The error is here: grade = inputFile.nextInt();

You are trying to read an int, but the file has no int at this location.

Citing from the documentation:

Scans the next token of the input as an int. This method will throw InputMismatchException if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched.