0

I am reading a text file to get the first Int, which is used to set the dimensions of a grid, and then it should read a series of rows of ints to populate that grid. I get the InputMismatchException on the thirdline of numbers. How do I get it to read in the numbers properly?

class Grid {
    protected int rows;// number of grid rows
    protected int cols;// number of grid columns

    protected boolean[][] grid;// the grid containing blobs
    boolean[][] visited;// used by blobCount

    public Grid(String fileName) throws FileNotFoundException {
        //reading file using scanner
        Scanner sc = new Scanner(new File(fileName));
        //first number is the dimension for rows
        this.rows = sc.nextInt();
        //since it is square matrix second dimension is same as first
        this.cols = this.rows;
        grid = new boolean[rows][cols];

        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++) {
                //reading from file 1 and 0 if 1 then store true else store false in grid                
                int number = sc.nextInt();
                if (number == 1 )
                    grid[i][j] = true;
                else
                    grid[i][j] = false;
            }
    }

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

        Scanner input = new Scanner(System.in);
        System.out.println("Enter filename: ");
        String fileName = input.nextLine();

        Grid grid = new Grid(fileName);

        // display grid and blob count
        System.out.println(grid);
        System.out.println("\nThere are " + grid.blobCount() + " blobs.\n");
    }
}

I should end up displaying the grid (15 lines of 15 numbers) followed by a count of blobs. The file contains the number 15 on the first line, an empty line, then 15 lines of 15 1's and 0's. I believe it is trying to read each line in as one int, but I am not sure if that's the case.

CFuentes
  • 15
  • 5
  • Do the 1s and 0s in each line have spaces between them? – RealSkeptic May 01 '19 at 14:35
  • "I believe it is trying to read each line in as one int" you have already answered your problem. – Matt May 01 '19 at 14:35
  • @Matt actually, if I give the input `1 2 3 4 5` and call `nextInt`, I would get `1`. If anything, it's the *empty* line that's causing the problem. – Benjamin Urquhart May 01 '19 at 14:38
  • @BenjaminUrquhart right, but op said that there were 15 lines of 15 1's and 0's, so the program is trying to read in a 15 digit number as an int, which is why the `InputMismatchException` occurs. See [nextInt](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()). – Matt May 01 '19 at 14:41
  • @Matt ah, I thought there were separators between the digits – Benjamin Urquhart May 01 '19 at 14:42
  • there are no separators. So if that is the issue, is there a way to read them in that way but still only one at a time? – CFuentes May 01 '19 at 16:03

0 Answers0