3

我对java还是很陌生。我为课堂创建了这个程序,它给了我一个以前从未遇到过的错误。如果有人可以提供帮助,那就太好了。谢谢!

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  

我收到此错误:

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

1 回答 1

3

错误在这里:grade = inputFile.nextInt();

您正在尝试读取一个 int,但该文件在此位置没有 int。

文档中引用:

将输入的下一个标记扫描为 int。如果下一个标记无法转换为有效的 int 值,则此方法将抛出 InputMismatchException,如下所述。如果翻译成功,扫描仪会超过匹配的输入。

于 2014-11-14T19:49:28.777 回答