我还是新手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)
答案 0 :(得分:3)
错误在于:grade = inputFile.nextInt();
您正在尝试读取int,但该文件在此位置没有int。
将输入的下一个标记扫描为int。如果下一个标记无法转换为有效的int值,则此方法将抛出InputMismatchException,如下所述。如果翻译成功,扫描仪将超过匹配的输入。