本文介绍了从.txt文件扫描时出现java.util.InputMismatchException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用2个类的程序.在一个类中,我创建了第二个类随后调用的方法.所有方法都包含在第一类中,而第二类仅调用它们并执行代码.

I am creating a program where 2 classes are used. In one class, i create methods that are then called by the second class. All methods are contained in the first class and the 2nd class simply calls them and executes the code.

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

...这有点奏效"语句只是为了查看它是否正确调用了该方法,看起来像是这样.

...the "It kinda works" statement is just to see if it was calling the method correctly, which it looks like it is.

据我了解,该错误告诉我它正在从.txt文件中读取错误的类型,但是idk为什么会这样.它甚至可以正确读取文件吗?任何形式的帮助都将是非常有用的,因为我已经盯着这个代码弄了好几个小时了!

To my understanding, the error is telling me that it is reading the wrong type from the .txt file, but idk why that would be. Is it even reading the file correctly? Any type of help would be great, as I have been staring and messing with this code for hours!

推荐答案

根据错误消息以及发生错误的位置,很可能您试图读取一个整数,但所读取的实际数据不是数字.

Based on the error message, and where the error occurs, most likely you are trying to read an integer, but the actual data that you are reading is not a number.

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

You could verify this by changing your scanner.nextInt() to a scanner.next() and printing out the value that you actually get. Alternatively, you could add "error handling" of the form:

    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;
    }

这篇关于从.txt文件扫描时出现java.util.InputMismatchException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:04