简体   繁体   中英

How do I fix a mismatch exception in Java?

I keep getting an error called "java.util.Mismatchexpection: null". I'm not sure how to fix this. It points to the grade = fileInput.nextInt(); line.

Here are the classes I'm using:

import java.util.Scanner;

import java.io.*;

public class Students
{
    public static void printAllStudents(Student[] students){
        for(int i=0;i<students.length;i++)
        {
            System.out.println(students[i]);
        }
    }

    public static void printAllStudentsByFirstName(Student[] students,String firstName){
        String name=firstName.split(" ")[1];
        int len=name.length();
        for (int i=0;i<students.length;i++){
            try{
                if(students[i].getFname().substring(0, len).equalsIgnoreCase(name)){
                    System.out.println(students[i]);
                }
            }
            catch (Exception e) {
                // TODO: handle exception
            }
        }
    }

    public static void printAllStudentsByLastName(Student[] students,String lastName){
        String name=lastName.split(" ")[1];

        int len = name.length();

        for(int i=0;i<students.length;i++){
            try{
                if(students[i].getLname().substring(0, len).equalsIgnoreCase(name)){
                    System.out.println(students[i]);
                }
            }
            catch (Exception e) {
                // TODO: handle exception
            }
        }
    }

    public static void printAllStudentsByGrades(Student[] students,String interval){
        int start=Integer.parseInt(interval.split(" ")[1]);
        int end=Integer.parseInt(interval.split(" ")[2]);
        for(int i=0;i<students.length;i++){
            if(students[i].getGrade()>=start && students[i].getGrade()<=end){
                System.out.println(students[i]);
            }
        }
    }

    public static void sort(Student[] students)
    {
        int n = students.length;
        int k;
        for (int m = n; m >= 0; m--) {
            for (int i = 0; i < n - 1; i++) {
                k = i + 1;
                if (students[i].getGrade() > students[k].getGrade()) 
                {
                    Student temp=new Student();
                    temp = students[k];
                    students[k]=students[i];
                    students[i]=temp;
                }
            }
        }
        printAllStudents(students);
    }

    public static void main (String[] args) throws IOException
    {
        String first_name, last_name, line = "";
        int grade, lines = 0, i;
        char ch;
        Scanner sc = new Scanner(System.in);
        Scanner fileInput = new Scanner(new File("students.txt"));
        //Counting the number of lines in the file.
        while (fileInput.hasNext())
        {
            line = fileInput.next();
            lines++;}

        fileInput = new Scanner(new File("students.txt"));
        //Declaring Student array of size lines.
        Student[] students=new Student[lines];
        i = 0;

        while (fileInput.hasNext())
        {
            first_name = fileInput.nextLine();
            last_name = fileInput.nextLine();
            grade = fileInput.nextInt();

            Student st = new Student(first_name, last_name, grade);
            students[i++] = st;
        }

        System.out.println("Please choose from below operations: ");
        System.out.println("1. printall");
        System.out.println("2. firstname <name>");
        System.out.println("3. lastname <name>");
        System.out.println("4. interval m n");
        System.out.println("5. sort");
        System.out.println("6. end");

        while (true)
        {
            System.out.println("Enter choice:");
            String choice = sc.nextLine();
            if(choice.equals("printall"))
                printAllStudents(students);
            else if(choice.indexOf("firstname")==0)
                printAllStudentsByFirstName(students, choice);
            else if(choice.indexOf("lastname") == 0)
                printAllStudentsByLastName(students, choice);
            else if(choice.indexOf("interval") == 0)
                printAllStudentsByGrades(students, choice);
            else if(choice.equals("sort"))
                sort(students);
            else if(choice.equals("end"))
                break;
        }
    }

}

    public class Student
{
    private String fname, lname;
    private int grade;

    public Student()
    {
        super(); //gives us the ability to override


    }

    public Student (String fname, String lname, int grade)
    {
        this.fname = fname;
        this.lname = lname;
        this.grade = grade;
    }

    public String getFname() {
        return fname;
    }

    public void setFname (String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public String toString()
    {
        return fname + " " + lname + "\t" + grade;
    }
}

Any help will be much appreciated.

A MismatchException is triggered when the Scanner runs into unexpected input. In your case, the Scanner is picking up null when it is expecting int input for fileInput.nextInt() .

This has to do with the formatting of student.txt and how you're parsing it; try running the code in debug, step-by-step, in your IDE (if you're using one) to pinpoint how to fix your text-file parsing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM