kylealanr
kylealanr

Reputation: 9

Scanners exception: java.util.InputMismatchException

I'm writing a program that accepts input from a file and prints a list of cities and their rainfall. I'm having trouble with the scanners that determine the lengths of the arrays need and the rainfall data for the cities.

I keep getting this Exception

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at BarChart.main(BarChart.java:29)

Here is my code:

import java.util.Scanner;

public class BarChart
{
    public static void main (String[] args)
    {
        //create scanner
        Scanner scan = new Scanner(System.in);

        //create size variable
        int size = scan.nextInt();

        //create arrays to hold cities and values
        String[] cities = new String [size];
        int[] values = new int [size];

        //input must be correct
        if (size > 0)
        {
            //set values of cities
            for(int i=0; i<size; i++)
            {
                cities[i] = scan.nextLine();
            }

            //set values of the data
            for(int j=0; j<size; j++)
            {
                values[j] = scan.nextInt();
            }

            //call the method to print the data
            printChart(cities, values);
        }
        //if wrong input given, explain and quit
        else
        {
            explanation();
            System.exit(0);
        }
    }

    //explanation of use
    public static void explanation()
    {
        System.out.println("");
        System.out.println("Error:");
        System.out.println("Input must be given from a file.");
        System.out.println("Must contain a list of cities and rainfall data");
        System.out.println("There must be at least 1 city for the program to run");
        System.out.println("");
        System.out.println("Example: java BarChart < input.txt");
        System.out.println("");
    }

    //print arrays created from file
    public static void printChart(String[] cities, int[] values)
    {
        for(int i=0; i<cities.length; i++)
        {
            System.out.printf( "%15s %-15s %n", cities, values);
        }
    }
}

Upvotes: 1

Views: 1806

Answers (3)

Paul Wagland
Paul Wagland

Reputation: 29106

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.

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

       for(int j=0; j<size; j++)
        {
          if (scan.hasNextInt()
            values[j] = scan.nextInt();
          else
            throw new RuntimeException("Unexpected token, wanted a number, but got: " + scan.next());
        }

Upvotes: 0

wullxz
wullxz

Reputation: 19430

Like in this question, you should also check if there's another token that matches your desired pattern (int).

Check with scanner.hasNextInt() before calling nextInt().

Upvotes: 0

Dennis Meng
Dennis Meng

Reputation: 5187

In your file, if the size of the list is the only thing on the first line, in other words, like this:

2
London
Paris
1
2

then when you enter the for loop to read in the city names, the Scanner hasn't read in the first newline yet. In the above example, the calls to newLine() will read a blank line and London, not London and Paris.

Hence, when you get to the second for loop to read in the rainfall data, the Scanner hasn't read in the last city yet (Paris in the above example), and will throw the InputMismatchException since the city name is clearly not a valid int.

Upvotes: 3

Related Questions