0

I have a file with some values in it:

11
8
0 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 0 0 0
0 0 1 1 1 1 1 1 1 0 0
0 1 1 0 1 1 1 0 1 1 0
1 1 1 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 0 1
1 0 1 0 0 0 0 0 1 0 1
0 0 0 1 1 0 1 1 0 0 0

I need to read those values into a 2D ArrayList. The fist two values (11 and 8) would be the number of rows and columns respectively. So here is the code:

            Scanner scanner = new Scanner(file);

            int x, y;

            x = scanner.nextInt();
            System.out.println(x + " has been read");

            y = scanner.nextInt();
            System.out.println(y + " has been read");


            ArrayList<ArrayList<Boolean>> pixelMap;
            pixelMap = new ArrayList<ArrayList<Boolean>>();
            ArrayList<Boolean> buffer_line = new ArrayList<Boolean>();

            Boolean buffer;


            for (int i = 0; i < x; i++){
                for (int j = 0; j < y; j++){
                    buffer = scanner.nextBoolean();
                    System.out.println(buffer + " has been read");
                    //buffer_line.add(buffer);
                }
                //pixelMap.add(buffer_line);
                //buffer_line.clear();
            }

The problem is - the program reads first two numbers successfully, and when it comes to boolean values, it throws InputMismatch exception on line

buffer = scanner.nextBoolean();

so I can't undersand why. 0 should be read next and it is boolean - so what's actually mismatching?

I also point out that if change buffer type to integer and then assign scanner.nextInt(), the program would read all the values properly, so in the output I would see all of them. So then of course, I can change ArrayList to Integer to make that work, but it would be semantically wrong as it would hold only boolean values. Can anyone help me to find out the problem?

Tony
  • 29
  • 8
  • 1
    In Java `boolean` does not have a numeric representation. – PM 77-1 Nov 08 '15 at 22:47
  • 1
    Boolean values are true/false. 1 and 0 are int values. – Andrew Lobley Nov 08 '15 at 22:48
  • Oh...that was quite unexpected, as I've got used to 0/1 representation in C++. So, what would be the best solution? Read the entire file as integers and then convert to boolean, or something else? – Tony Nov 08 '15 at 22:54
  • 1
    @Tony you could just do `buffer = scanner.nextInt()>0?true:false;`. Also FYI using `buffer_line.clear()` will clear out all the buffers in your `pixelMap` because you never created a new object (they are all the same ArrayList). Upon making a new line make your `buffer_line` a new Object `buffer_line = new ArrayList();` – ug_ Nov 08 '15 at 23:00
  • @ug_ thank you so much. I've just forgot that there is no copy constructor in Java unlike C++. Need some time to think over my programming logic. :) – Tony Nov 08 '15 at 23:15
  • 1
    @Tony Dont forget with Java to take advantage of the great IDE support. Most IDEs support hovering to show java documentation or `ctrl+right click` to navigate into methods so you can look at their source. Using these to your advantage can save alot of time and help resolve issues much faster :) – ug_ Nov 08 '15 at 23:25

1 Answers1

1

In your code you have this statement:

buffer = scanner.nextBoolean();

But I do not see boolean values true or false in the input file.

In Java, 0 and 1 are not treated as boolean values like in other languages such as C.

You need to read these values as int and then manually map them to boolean values.

Logic something like this:

int val = scanner.nextInt();
boolean buffer = (val == 1) ? true : false;
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38