Konorika
Konorika

Reputation: 79

Everything compiled fine, but has exception error

This is school lab project which read text file from C:/Maze/maze.text, and it would replace "." to space if it's right path, and replace it to "v" if it's dead end path. This is the code.

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

public class Maze{
   private String[][] maze;
   private int rows;
   private int cols;
   private int startX;
   private int startY;
   private int endX;
   private int endY;
   private Scanner scan;

   public Maze (File filename){
      Scanner scan;
      try{
         scan = new Scanner(filename);
      }
      catch(FileNotFoundException e){
         System.out.println ("File not found");
         return;
      }
      rows = scan.nextInt();
      cols = scan.nextInt();
      maze = new String[rows][cols];
      startX = scan.nextInt();
      startY = scan.nextInt();
      for (int i = 0; i < rows; i++){
         String temp = scan.next();
         for (int j = 0; j < cols; j++){
            maze[i][j] = "" + temp.charAt(j);
         }
         scan.close();
      }
   }

   public String toString(){
      String temp = "";
      for (int i = 0; i < rows; i++){
         for (int j = 0; j < cols; j++){
            temp = temp + maze[i][j] + " ";
         }
         temp = temp + '\n';
      }
      return temp;
   }
   public boolean search(){
      return searchMaze(startX, startY);
   }

   public boolean searchMaze(int i, int j){
      boolean mazeFound = false;      
      boolean deadEnd = false;
      String up = maze[i][j-1];
      String down = maze [i][j+1];
      String right = maze [i+1][j];
      String left = maze [i-1][j];
      if (maze[i][j] == maze[endX][endY]){
         mazeFound = true;
      }  
      else {
         while (!deadEnd){
            if (up == "."){
               up = " ";
               searchMaze(i-1, j);
            }
            else if (down == "."){
               down = " ";
               searchMaze(i+1, j);
            }
            else if (right == "."){
               right = " ";
               searchMaze(i, j+1);
            }
            else if (left == "."){
               left = " ";
               searchMaze(i, j-1);
            }
            else {
            deadEnd = true;
            searchMaze (i, j);
            }
         }
         while (deadEnd){
            if (up == "."|| down == "." || right == "." || left == "."){
               deadEnd = false;
               searchMaze (i, j);
            }

            else if (up == " "){
               maze[i][j] = "v";
               searchMaze (i-1, j);
            }
            else if (down == " "){
               maze[i][j] = "v";
               searchMaze (i+1, j);
            }            
            else if (right == " "){
               maze[i][j] = "v";
               searchMaze (i, j+1);
            }         
            else if (left == " "){
               maze[i][j] = "v";
               searchMaze (i, j-1);
            }
         }
      } 
      return mazeFound;
   }
}

and this is my testing class.

import java.io.File;

public class MazeRunner{
   public static void main(String[] args){
      Maze maze = new Maze(new File("c:/Maze/maze.txt"));
      System.out.println(maze);
      maze.search();
      System.out.println(maze);
   }
}

if I run this code, then it shows message like this .

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 Maze.<init>(Maze.java:24)
    at MazeRunner.main(MazeRunner.java:5)

Do you guys have any idea why this message is shown?

I also made new text name "maze" in C:/Maze/, and it looks like

x x x x x x x x x x x x x x x x x x x x x x x x x 
. . . . . . . . . . . . x x x x x x x x x x x x x 
x x . x x x . x x x x x x x x x x x x x x x x x x 
x x . x x x x x x x x x x x x x x x x x x x x x x 
x x . . . . . . . . . x x x x x x x x x x x x x x 
x x . x x x x x x x x x x x x x x x x x x x x x x 
x x . x x x x x x x x x x x x x x x x x x x x x x 
x x . x x x x x x x x x x x x x x x x x x x x x x 
x x . . . . . . . . . . . . . . . . . x x x x x x 
x x x x x . x x x x x x x x x . x x x x x x x x x 
x x x x x . x x x x x x x x x . x x x x x x x x x 
x x x x x . x x x x x x x x x . . . . x x x x x x 
x x x x x . x x x x x x x x x x x x x x x x x x x 
x x x x x . x x x x x x x x x x x x x x x x x x x 
x x x . . . x x x x x x x x x x x x x x x x x x x 
x x x x x . x x x x x x x x x x x x x x x . x x x 
x x x x x . x x x x x x x x x x x x x x x . x x x 
x x x x x . x x x x x x x x x x x x x x x . x x x 
x x x x x . . . . . . . . . . . . . . . . . x x x 
x x x x x . x x x x x x x x x x . x x x x x x x x 
x x x x x . x x x x x x x x x x . x x x x x x x x 
x x x x x . x x x x x x x x x x . x x x x x x x x 
x x x x x . x x x x x x x x x x . x x x x x x x x 
x x x x x . x x x x x x x x x x . . . . . . . . . 
x x x x x x x x x x x x x x x x x x x x x x x x x

and those "." didn't replaced even I run this.

Upvotes: 1

Views: 92

Answers (2)

Yassin Hajaj
Yassin Hajaj

Reputation: 21975

I assume you want to enter manually the number of lines/columns + posX & posY ?

Here's one solution

 private Scanner scan;
 private Scanner keyboard = new Scanner(System.in);

   public Maze (File filename){
      // Scanner scan; This line is def. not necessary
      try{
         scan = new Scanner(filename);
      }
      catch(FileNotFoundException e){
         System.out.println ("File not found");
         return;
      }
      rows = keyboard.nextInt();
      cols = keyboard.nextInt();
      maze = new String[rows][cols];
      startX = keyboard.nextInt();
      startY = keyboard.nextInt();
      for (int i = 0; i < rows; i++){
         String temp = scan.next();
         for (int j = 0; j < cols; j++){
            maze[i][j] = "" + temp.charAt(j);
         }
         scan.close();
         keyboard.close();
      }
   }

Upvotes: 0

user2238635
user2238635

Reputation: 56

rows = scan.nextInt();

You call nextInt() in the code, but the first character in the file is not an integer, so it will throw an InputMismatchException exception.

Upvotes: 1

Related Questions