Excluding input mismatch?

I'm still pretty new to java. I created this program for a class and it gave me an error that I have never received before. If anyone can help, that would be great. Thank!

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 am 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)

      

+3


source to share


1 answer


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

You are trying to read an int, but the file does not have an int at that location.



Quoting from the documentation :

Scans the next input token as an int. This method will throw an 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.

+3


source







All Articles