Hi everyone,

I am using this method to read in a collection of ints and strings then converting the strings to numbers using the switch statement(as stated by my lecturer) and save it back into an array for further processing. However, everytime it gets to the line studentResponses[row][column] = scanner2.nextInt(); I get the error: java.util.InputMismatch exception: null (in java.util.Scanner). I have spent so long trying to figure out what could be causing this error to no avail so any guidance at all would be appreciated :)

public void readMarksData(String fileName, int numberOfStudents, int responsesPerStudent) throws FileNotFoundException
{
    studentResponses = new int[numberOfStudents][responsesPerStudent];
    File dataFile = new File(fileName);

    Scanner scanner = new Scanner (dataFile);

    cohort = scanner.nextLine();
    int numberOfResponses = scanner.nextInt();
    System.out.println (cohort);
    System.out.println (numberOfResponses);
    scanner.useDelimiter(",");      

    for (int row = 0; row < studentResponses.length; row++)
    {
        Scanner scanner2 = new Scanner(scanner.nextLine());
        for (int column = 0; column < studentResponses[0].length-4; column++)
        {
            scanner2.useDelimiter(",");
            **studentResponses[row][column] = scanner2.nextInt();**

        }
        System.out.print("\t");
        for (int column = studentResponses[0].length-4; column < studentResponses[0].length; column++)
        {
             scanner2.useDelimiter("[ ]*(,)[ ]*");
             String feedback = scanner2.next();
             switch (feedback.toLowerCase())
             {
                 case ("Excellent"): studentResponses[row][column] = 5;
                 break;
                 case ("Very Good"): studentResponses[row][column] = 4;
                 break;
                 case ("Good"): studentResponses[row][column] = 3;
                 break;
                 case ("Average"): studentResponses[row][column] = 2;
                 break;
                 default: studentResponses[row][column] = 1;
                 System.out.print(studentResponses[row][column]);
             }    
        }

        scanner2.close(); 
    }
    scanner.close(); 
}

Recommended Answers

All 2 Replies

hai scippi,

mostly this kind of situation will occur by entering/providing wrong input values (the exception name itself saying to us clearly you entered input value is other than what you expecting from the code .......)

i think you have entered the value other than the integer value so it thrown the following exception to you other than that no issues at all in the code.

java.util.InputMismatch exception: null (in java.util.Scanner)

go through the url it explains the exception details

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/InputMismatchException.html

you should check your scanner.nextLine()
for null
the is a mismatch b/n the first scanner reader and the
the second file i suggest you use inputstream with a string reader

that is more convinent and you will have a
greater grasp of the input output exceptions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.