Search code examples
javainputmismatchexception

Reading from an Input File to an Integer Array


I'm currently coding a project in Java that will take an input file and read it into several parallel arrays. There are several restrictions--we can't use array lists, files must be read using Scanner. After reading it into the arrays, there are a few other steps that I need to code, but I have hit a hangup.

    public static void main(String[] args) throws FileNotFoundException {

    final int ARRAY_SIZE = 10;
    int choice;
    int i, variableNumber;
    String[] customerName = new String[ARRAY_SIZE];
    int[] customerID = new int[ARRAY_SIZE];
    String[] os = new String[ARRAY_SIZE];
    String[] typeOfProblem = new String[ARRAY_SIZE];
    int[] turnAroundTime = new int[ARRAY_SIZE];

    readFile(customerName, customerID, os, typeOfProblem, turnAroundTime);

}

public static void readFile(String[] customerName, int[] customerID, String[] os, String[] typeOfProblem, int[] turnAroundTime) throws FileNotFoundException
{
    File hotlist = new File("hotlist.txt");
    int i = 0;

    if (!hotlist.exists())
    {
        System.out.println("The input file was not found.");
        System.exit(0);
    }
    Scanner inputFile = new Scanner(hotlist);
    while (inputFile.hasNext())
    {
        customerName[i] = inputFile.nextLine();
        System.out.println(customerName[i]);
        customerID[i] = inputFile.nextInt();
        os[i] = inputFile.nextLine();
        typeOfProblem[i] = inputFile.nextLine();
        turnAroundTime[i] = inputFile.nextInt();
        i++;
    }
    System.out.println("This is only a test." + customerName[1] + "\n" + customerID[1] + "\n"
                        + os[1] + "\n" + typeOfProblem[1] + "\n" + turnAroundTime[1]);
}

When I try to run the above code, it fails with the following errors:

run:
Mike Rowe
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at mckelvey_project3.McKelvey_Project3.readFile(McKelvey_Project3.java:70)
    at mckelvey_project3.McKelvey_Project3.main(McKelvey_Project3.java:33)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

The contents of the hotlist.txt file are as follows:

Mike Rowe
1
Windows DOS
Too Much ASCII Porn
3
Some Guy
2
Windows 10
Too Much Windows
200

Any help is greatly appreciated! By the way, all the System.out statements are test statements as I was trying to debug my code. I've isolated the error to specifically

customerID[i] = inputFile.nextInt();

and similarly

turnAroundTime[i] = inputFile.nextInt();

but can't figure out why those statements aren't working.


Solution

  • When you call Scanner.nextInt() it only consume the int, and it leaves any trailing whitespace or newline there. Instead you might use something like,

    Scanner inputFile = new Scanner(hotlist);
    while (inputFile.hasNext()) {
        customerName[i] = inputFile.nextLine();
        System.out.println(customerName[i]);
        String custId = inputFile.nextLine();
        customerID[i] = Integer.parseInt(custId);
        os[i] = inputFile.nextLine();
        typeOfProblem[i] = inputFile.nextLine();
        String turnAround = inputFile.nextLine();
        turnAroundTime[i] = Integer.parseInt(turnAround);
        i++;
    }
    

    And I get (with your code / file),

    Mike Rowe
    Some Guy
    This is only a test.Some Guy
    2
    Windows 10
    Too Much Windows
    200