2

I am trying to get a sudoku verifier working by reading in txt file with comma-seperated digits. My code reads the first line into the array then when going to the next line throws an InputMismatchException error. I've tried looking things up but nothing has worked. This is my code along with what the txt input file contains

String fileName = "data/sudoku-valid-1.txt";
    Scanner s = null;
    try {
        s = new Scanner(new File(fileName));
        s.useDelimiter(",");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    int array[][] = new int[9][9];
    int i, j;
    while(s.hasNext()) {
        for(i=0; i<9; i++) {
            for(j=0; j<9; j++) {
                array[i][j] = s.nextInt();
            }
        }
    }

Txt file:

1,5,7,9,8,3,6,2,4,

2,6,8,7,5,4,9,3,1,

9,4,3,6,1,2,7,5,8,

8,9,2,1,7,6,5,4,3,

3,1,6,5,4,8,2,7,9,

5,7,4,3,2,9,1,8,6,

7,3,5,4,6,1,8,9,2,

6,8,9,2,3,5,4,1,7,

4,2,1,8,9,7,3,6,5,
DAIRAV
  • 723
  • 1
  • 9
  • 31
  • 1
    Try adding a s.nextLine() after the inner for loop. – Shar1er80 Jul 17 '18 at 05:34
  • Or use a `delimiter` that also manage the "newline" and "empty line". It accept a Pattern or a regex `String` – AxelH Jul 17 '18 at 05:39
  • By the way your array declaration should be `int[][] array = new int[9][9]` – sher222 Jul 17 '18 at 05:43
  • 2
    @sher222 not necessarily ... both are valid. The difference is if you declare more than one variable at the same time `int[] a1, a2` and `int a1[], a2` don't declare the same variables. – AxelH Jul 17 '18 at 05:43

3 Answers3

0

With scanner, you need to check if there is next element. In your case when you reach end of the line, you are getting empty String, that you want to cast to Integer. In this case you should simply skip to next scanned element.

int[][] array = new int[9][9];
try (Scanner s = new Scanner(new File(fileName))) {
  s.useDelimiter(",");

  int i, j;
  while (s.hasNext()) {
    for (i = 0; i < 9; i++) {
      for (j = 0; j < 9; j++) {
        if (s.hasNextInt())
          array[i][j] = s.nextInt();
        else if(s.hasNext())
          s.next();    
      }
    }
  }
} catch (FileNotFoundException e) {
  e.printStackTrace();
}           

Secondly remember to close your scanner. In above example I have used try-catch-resources to do that.

Beri
  • 11,470
  • 4
  • 35
  • 57
0

Use a delimiter that also manage the new lines (spaces, ...). Scanner.useDelimiter accepts regex

,\s*

Visible with Regex101

This have the advantage that you don't have to bother to know what format will have the data, you read it without having to check anything.

Example :

String s = "1,2,3,4,5,"
        + "\n6,7,8,9,10";
Scanner scan = new Scanner(s);
scan.useDelimiter(",\\s*");

while(scan.hasNext()){
    System.out.println(scan.nextInt());
}

scan.close();

Output :

1
2
3
4
5
6
7
8
9
10
AxelH
  • 14,325
  • 2
  • 25
  • 55
0

Adding s.nextLine() after the inner for loop should resolve your issue. It'll read the cr/lf and move to the next line for reading.

import java.util.Scanner;
import java.io.FileNotFoundException;

public class MyClass {
    public static void main(String[] args)
    {
        String fileName = "1,5,7,9,8,3,6,2,4,\r\n" +
                            "2,6,8,7,5,4,9,3,1,\r\n" +
                            "9,4,3,6,1,2,7,5,8,\r\n" +
                            "8,9,2,1,7,6,5,4,3,\r\n" +
                            "3,1,6,5,4,8,2,7,9,\r\n" +
                            "5,7,4,3,2,9,1,8,6,\r\n" +
                            "7,3,5,4,6,1,8,9,2,\r\n" +
                            "6,8,9,2,3,5,4,1,7,\r\n" +
                            "4,2,1,8,9,7,3,6,5,";
        Scanner s = null;
        try {
            s = new Scanner(fileName);
            s.useDelimiter(",");

        } catch (Exception e) {
            e.printStackTrace();
        }

        int array[][] = new int[9][9];
        int i, j;
        while(s.hasNext()) {
            for(i=0; i<9; i++) {
                for(j=0; j<9; j++) {
                    array[i][j] = s.nextInt();
                }
                s.nextLine();
            }
        }

        for (i = 0; i < array.length; i++) {
            for (j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]);
            }
            System.out.println();
        }

        s.close();
    }
}

Result:

157983624
268754931
943612758
892176543
316548279
574329186
735461892
689235417
421897365
Shar1er80
  • 9,001
  • 2
  • 20
  • 29