1

i got a maze from a file i try to make this Write a class Exercise4 with a program that reads such a maze file into a two-dimensional boolean array. Then display the array on the console with one line for each row. Represent array elements using blank-symbols and #-symbols so that the console output has the same format as the maze file, see the example above.

    package assignmentce152;


    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    import java.io.FileNotFoundException;
/**
 * Created by ak on 29/03/2017.
 */
public class Exercise4 {
    public static void main(String[] args) throws IOException {
        Mazes();
    }
        private static char[][] maze = null;
        private static int rows = 0;
        private static int cols = 0;
        private static int xStart = 0;
        private static int yStart = 0;

        public static void Mazes() throws IOException {
            File mazefile = new File("C:/Users/IdeaProjects/Assignment152/data/maze21.txt");
            BufferedReader reader = new BufferedReader(new FileReader(mazefile));

           Scanner lineOfFile = new Scanner(reader.readLine()); 

            rows = lineOfFile.nextInt(); //get the number of rows of the maze

            cols = lineOfFile.nextInt(); // get the number of columns of the maze
            maze = new char[rows][cols]; //create a char array of the proper size

            //For loops to iterate the rows and col to find the start/enterance of the maze as it pertains to the first char in the row
            for (int y = 0; y < cols; y++) {
                lineOfFile = new Scanner(reader.readLine());
                for (int x = 0; x < rows; x++) {
                    char start = lineOfFile.next().charAt(0);
                    maze[x][y] = start;

                    //statement to set the starting coorinates for the maze
                    if (start == '.') {
                        xStart = x;
                        yStart = y;
                    }

                }
            }


        }
    }

how ever i got these ERROR what should i change ? Anything i think will be helpful

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 assignmentce152.Exercise4.Mazes(Exercise4.java:28)
at assignmentce152.Exercise4.main(Exercise4.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
azurefrog
  • 10,785
  • 7
  • 42
  • 56
Fizz
  • 37
  • 8
  • http://stackoverflow.com/questions/21143028/exception-in-thread-main-java-util-inputmismatchexception – Thelouras Mar 29 '17 at 16:33
  • Well to be honest i cant figure how to fix it in my own program. because is not very similar – Fizz Mar 29 '17 at 16:54

1 Answers1

0

It seems your program tries to read two integers from the scanner and does not find two integers there.

Let me guess: the following line is wrong.

       Scanner lineOfFile = new Scanner(reader.readLine()); 

readLine() reads the first line of your file. From this line you create a scanner object that could be used for parsing the line. If, for instance, your two numbers are on the first two lines of your file, it will never get to them both and will throw an InputMismatchException.

Instead you can try discarding the reader and just creating your scanner as

       Scanner lineOfFile = new Scanner(mazefile); 

This will allow your scanner to read the entire text file rather than only the first line.

If this doesn’t solve your problem, I believe you need to edit your question and add a sample of how your file looks.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161