一切编译得很好,但有异常错误

时间:2015-09-17 00:12:54

标签: java exception

这是从C:/Maze/maze.text中读取文本文件的学校实验室项目,它将取代"。"如果它是正确的路径,请将其替换为" v"如果它是死路径。这是代码。

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;
   }
}

这是我的测试课。

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);
   }
}

如果我运行此代码,那么它会显示这样的消息。

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)

你们有没有想过为什么要显示这条消息?

我还制作了新的文字名称&#34;迷宫&#34;在C:/ Maze /中,它看起来像

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

和那些&#34;。&#34;即使我跑了,也没有取代。

2 个答案:

答案 0 :(得分:1)

rows = scan.nextInt();

您在代码中调用nextInt(),但文件中的第一个字符不是整数,因此它将抛出InputMismatchException异常。

答案 1 :(得分:0)

我假设你想手动输入行数/列数+ posX&amp;你好吗?

这是一个解决方案

 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();
      }
   }