从.txt文件扫描时出现java.util.InputMismatchException错误

时间:2013-10-18 22:58:54

标签: java methods

我正在创建一个使用2个类的程序。在一个类中,我创建了由第二个类调用的方法。所有方法都包含在第一个类中,第二个类只是调用它们并执行代码。

第1类

 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;


 public class Student {


    private Scanner scanner;
    private String firstName;
    private String lastName;
    private int homeworkScore;
    private int testScore;
    private String letterGrade;
    private int numberOfStudents;

    public Student () {

        String firstName = null;
        String lastName = null;
        int homeworkScore = 0;
        int testScore = 0;
        String letterGrade = null;
        int numberOfStudents = 0;
    }


    public void openFile(){
        try { 
            scanner = new Scanner(new File("grades.txt"));
        } catch (FileNotFoundException e) { 
            System.out.println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class");
            System.exit(0); 
        }
    }


    public void setNumberOfStudents() {
        System.out.println("It kinda works");
        numberOfStudents = scanner.nextInt();
    }

 public void setFastName() {
        fastName = scanner.next();
    }

 public void setLastName() {
        lastName = scanner.next();
    }

public void setHomeworkScore() {

        int subAssignment = 0;
        int assignment = 0;

        for(int i = 1; i <= 21; i++) { 
            subAssignment = scanner.nextInt();
            assignment += subAssignment;
        }

        homeworkScore = assignment;
    }

第2类

  import java.io.File;
  import java.io.FileNotFoundException;
  import java.util.Scanner;


 public class CourseGrade {

public static void main(String[] args) {

    Student myStudent = new Student();

    myStudent.openFile();

    myStudent.setNumberOfStudents();

    myStudent.setFirstName();

    myStudent.setLastName();

    myStudent.setHomeworkScore();


}

 }

这是我得到的错误:

 It kinda works
 Exception in thread "main" 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 Student.setHomeworkScore(Student.java:54)
 at CourseGrade.main(CourseGrade.java:20)

...“It kinda works”语句只是为了看它是否正确地调用了方法,它看起来就像是。

根据我的理解,错误告诉我它正在从.txt文件中读取错误的类型,但是idk为什么会这样。它甚至正确读取文件?任何类型的帮助都会很棒,因为我几个小时都在盯着这个代码搞乱!

2 个答案:

答案 0 :(得分:0)

根据oracle java文档,抛出该异常,when the token doesn't fit the requested pattern (int) or is out of range

如果文件中没有更多的int,则抛出该异常 您可以通过检查是否有另一个int值来使用扫描程序方法hasNextInt来避免该异常。

例如:

for(int i = 1; i <= 21 && scanner.hasNextInt(); i++) { 
            subAssignment = scanner.nextInt();
            assignment += subAssignment;
}

(如果这不能解决您的问题,您还应该包括您的输入文件)

答案 1 :(得分:0)

根据错误消息以及发生错误的位置,您很可能尝试读取整数,但您正在读取的实际数据不是数字。

您可以通过将scanner.nextInt()更改为scanner.next()并打印出实际获得的值来验证这一点。或者,您可以添加表单的“错误处理”:

    for(int i = 1; i <= 21; i++) { 
        if (scanner.hasNextInt()
          subAssignment = scanner.nextInt();
        else
          throw new RuntimeException("Unexpected token, wanted a number, but got: " + scanner.next());
        assignment += subAssignment;
    }