0

Hi I am trying to read an input.txt file by using a scanner, but I keep getting the input mismatch exception and I am unsure why. The file that I am reading in is formatted like this: first is a single number to identify array size. The next line is a list of integers delimited by commas. This is what I have but it fails on the first integer being read in:

     File inputFile = new File("input.txt");
     Scanner scan = new Scanner(inputFile);
     int arraySize = scan.nextInt();
     scan.nextLine();
     int[] array = new int[arraySize];
     for (int i = 0; i < arraySize; i++) { 
        array[i] = scan.nextInt();
     }

I also think I will probably need something in there to catch the commas after each int. Maybe scan.next(",")? but it is failing before the first comma.

Thanks in advance!

EDIT: input file for example:

5
-1, -2, -3 , -4, -5
user6287161
  • 5
  • 1
  • 6
  • Can you post the input file, or at least the beginning? For the commas, see [How to split a string in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java). – bradimus Nov 10 '16 at 19:34
  • a quick fix would be to, after your `scan.nextLine` use `scan.useDelimeter(",")`, which should ignore commas and use every int in between when getting the next input – Zircon Nov 10 '16 at 19:35
  • I posted the example input file, but debugging showed that the error is occurring before the first comma, I added in the comma delimiter and it still does not get passed the first value. – user6287161 Nov 10 '16 at 19:41

3 Answers3

0
File inputFile = new File("C:\\file.txt");
    Scanner scan = new Scanner(inputFile);
    String size = scan.nextLine(); // read size
    String aux = scan.nextLine(); 
    aux = aux.replaceAll("\\s",""); // remove whitespaces for better Integer.parseInt(String)
    String[] parts = aux.split(","); 
    System.out.println("size "+size);
    for (int i = 0; i < parts.length; i++) { 
       System.out.println(parts[i]);
    }
    scan.close();

Then you can convert the strings to integers.

Oscar Martinez
  • 621
  • 1
  • 8
  • 18
0

Your problem is that calling scanner.nextInt() delimits the elements with a space. There are two things you can do to fix this: you can either set the delimiter to be ", " (scanner.useDelimiter(", ");) or see Oscar M's answer.
Example:

Scanner sc = new Scanner("-1, -2, -3, -4");
sc.useDelimiter(", ");
System.out.println(sc.nextInt());
System.out.println(sc.nextInt());

Output:

-1
-2
James McDowell
  • 2,668
  • 1
  • 14
  • 27
  • Even doing this for some reason it gives me the same error when i = 2. I just don't understand how it can work for the first few but not the rest. Is it something to do with the negative signs by chance? – user6287161 Nov 10 '16 at 19:52
  • Make sure the comma character you used is the one in the file. – James McDowell Nov 10 '16 at 20:07
0

You need to specify the delimiter that you read the string. It defaults to consume only whitespace, not commas.

public static void main (String[] args)
{
    int size = 5;
    Scanner sc = new Scanner("-1, -2, -3, -4, -5");
    sc.useDelimiter("\\s*,\\s*"); // commas surrounded by whitespace
    for (int i = 0; i < size; i++) {
        System.out.println(sc.nextInt());
    }
}

Example

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245