在我的程序中,我正在尝试使用JFileChooser选择输入文件并在colsole中显示输出。如果我在该文件中选择grade.txt文件,我会有不同的等级,如:
20 98.83 40.05 0.97 9.77 87.73 87.80 99.15 94.77 41.39
输出看起来像:
Student 1 score is 98.83 and grade is A
Student 2 score is 40.05 and grade is F
Student 3 score is 0.97 and grade is F
当我试图运行我的程序时,我在线程“main”中得到了Exception
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Homework10.A.main(A.java:23)
我该如何解决此错误?
public class A {
public static void main(String[] args) throws FileNotFoundException {
Scanner user = new Scanner(System.in);
JFileChooser jfc = new JFileChooser(".");
int jfcUserOption = jfc.showOpenDialog(null);
if (jfcUserOption == JFileChooser.APPROVE_OPTION){
File chosenFile = jfc.getSelectedFile();
Scanner input = new Scanner( chosenFile );
while(input.hasNext()){
int n = input.nextInt();
int bestScore = 0;
int[] score = new int[n];
for (int i = 0; i < score.length; i++) {
score[i] = input.nextInt();
if (bestScore < score[i]) {
bestScore = score[i];
}
}
char grade;
for (int i = 1; i < score.length; i++) {
if (score[i] >= bestScore - 10)
grade = 'A';
else if (score[i] >= bestScore - 20)
grade = 'B';
else if (score[i] >= bestScore - 30)
grade = 'C';
else if (score[i] >= bestScore - 40)
grade = 'D';
else
grade = 'F';
System.out.println("Student " + i +" score is " + score[i] +" and grade is " + grade);
}
}
}
}
}