2013-10-04 31 views
-1

嗨即時通訊新的Java和即時通訊試圖編寫一個解決迷宮的程序。但運行程序時,我不斷收到一個錯誤:Java迷宮程序中的異常錯誤

Exception in thread "main" java.util.InputMismatchException 
    at java.util.Scanner.throwFor(Unknown Source) 
    at java.util.Scanner.next(Unknown Source) 
    at java.util.Scanner.nextInt(Unknown Source) 
    at java.util.Scanner.nextInt(Unknown Source) 
    at assignment4b.Maze.<init>(Maze.java:38) 
    at assignment4b.Question5.main(Question5.java:10) 

38號線在那裏說

assert (readFile) != null;

線10在那裏說

Maze m = new Maze("Maze.txt");

這是我如何閱讀文件看起來像:

20 7 Number of columns and rows 
    0 18 Exit square (rows and columns start at 0) 
    6 12 Entrance square 
    XXXXXXXXXXXXXXXXXX X 
    X   XXXX X 
    X XXXXX XXXXX XX X 
    X XXXXX XXXXXXX XX X 
    X   XX XX X 
    X XXXXXXXXXX XX X 
    XXXXXXXXXXXX XXXXXXX 

這是怎麼了我的主要的樣子:

import java.util.Scanner; 

    public class Question5 
    { 
     public static void main (String[]args) 
     { 
      // Construct the maze 
      Maze m = new Maze("Maze.txt"); 
      //System.out.println(m); 

      while (!m.isOut()) 
      { 
        // input from the keyboard 
        int move; 
        Scanner kb = new Scanner(System.in); 
        System.out.print("Your move (1:north, 2:west, 3:east, 4:south, 5:help): "); 
        move = kb.nextInt(); 

        System.out.println(); 

        // Process the player's selection 
        switch (move) 
        { 
        case '1': 
         m.goNorth(); 
         break; 
        case '2': 
         m.goWest(); 
         break; 
        case '3': 
         m.goEast(); 
         break; 
        case '4': 
         m.goSouth(); 
         break; 
        case '5': 
         m.showPath(System.out); 
         System.out.println(); 
         break; 
        } 


        // Display the new maze (unless help was selected) 
        if (move != 5) 
        { 
         System.out.print(m); 
         System.out.print("\n"); 
        } 
      } 
     } 
    } 

這是我的迷宮類看起來像:

import java.io.OutputStream; 
import java.util.Scanner; 

public class Maze implements MazeInterface 
{ 

     public static final int CLEAR = 0; 
     public static final int WALL = 1; 
     public static final int VISITED = 2; 
     public static final int PATH = 3; 

     private int row; 
     private int col; 
     // 2D array to represent the maze (on the heap) 
     private int[][] maze; 

     // entrance and exit points of the maze 
     private Location entrance = new Location(); 
     private Location exit = new Location(); 

     // Location of the creature 
     private Location creature = new Location(); 

     public Maze(String filename) 
     { 


       //innitialize maze 
       // maze = col<row<ReadFile>>(); 
       // input >> col >> row; 

       //print path of file 
       //System.out.println(file.getAbsolutePath()); 
       Scanner readFile = new Scanner (filename); 

       assert (readFile) != null; 
       row = readFile.nextInt(); 
       col= readFile.nextInt();  
      // row and col must be positive 
      assert(row>0 && col>0); 

     // Dump the rest of the line 
      while(readFile.hasNextLine()); 

       // Maze exit 
       exit.r = readFile.nextInt(); 
       exit.c = readFile.nextInt(); 

       // exit.r and exit.c must be between row and col 
       assert(exit.r>=0 && exit.r<row); 
       assert(exit.c>=0 && exit.c<col); 

      // Dump the rest of the line 
      while(readFile.hasNextLine()); 

       // Maze exit 
       exit.r = readFile.nextInt(); 
       exit.c = readFile.nextInt(); 

       // exit.r and exit.c must be between row and col 
       assert(exit.r>=0 && exit.r<row); 
       assert(exit.c>=0 && exit.c<col); 

       // Maze entry 
       entrance.r = readFile.nextInt(); 
       entrance.c = readFile.nextInt(); 
       // entrance.r and entrance.c must be between row and col 
       assert(entrance.r>=0 && entrance.r<row); 
       assert(entrance.c>=0 && entrance.c<col); 


       // Dump the rest of the line 
       while(readFile.hasNextLine()); 


       // Maze structure 
       // Create the maze on the heap: 2D array of row*col ints 
       maze = new int[row][col]; 
       for(int i=0; i<row; ++i) 
        maze[i] = new int[col]; 

       // Read the maze line by line 
       String mazeLine; 
       for(int r=0; r<row; ++r) 
       { 
        // eof must not have been reached 
        assert(readFile != null); 
        mazeLine=readFile.toString(); 

        for(int c=0; c<col; ++c) 
        { 
         // mazeline must be made of 'X' and ' ' only 
         assert(mazeLine.charAt(c) ==' ' || mazeLine.charAt(c) =='X'); 
         maze[r][c] = ((mazeLine.charAt(c) ==' ')?CLEAR:WALL); 
        } 
       } 

       // The entrance and exit must not be blocked 
       assert(maze[entrance.r][entrance.c]!=WALL && 
         maze[exit.r][exit.c]!=WALL); 

       // The creature is at the entrance 
       creature = entrance; 



     } 

     public boolean goNorth() 
     { 
      // Location north of the creature 
      Location north = creature; 
      north.r--; 
      // if north is inside of the maze and the square 
      // at north is not a wall, we can move the creature 
      if (insideMaze(north) && square(north) != WALL) 
      { 
       creature = north; 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 

     public boolean goSouth() 
     { 
      // Location south of the creature 
      Location south = creature; 
      south.r++; 
      // if south is inside of the maze and the square 
      // at south is not a wall, we can move the creature 
      if (insideMaze(south) && square(south) != WALL) 
      { 
       creature = south; 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 

     public boolean goWest() 
     { 
      // Location west of the creature 
      Location west = creature; 
      west.c--; 
      // if west is inside of the maze and the square 
      // at west is not a wall, we can move the creature 
      if (insideMaze(west) && square(west) != WALL) 
      { 
       creature = west; 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 

     public boolean goEast() 
     { 
      // Location east of the creature 
      Location east = creature; 
      east.c++; 
      // if east is inside of the maze and the square 
      // at east is not a wall, we can move the creature 
      if (insideMaze(east) && square(east) != WALL) 
      { 
       creature = east; 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 

     public boolean isOut() 
     { 
      return (creature == exit); 
     } 

     public void showPath(OutputStream out) 
     { 
      // =there is a path 
      boolean success = false; 

      // Look for the path recursively 
      findPath(creature,success); 

      // print the path (if we found one0 
      if (success) 
      { 
       System.out.println(this); 
      } 
      else 
      { 
       System.out.println("There is no solution path"); 
      } 

      // Remove the VISITED and PATH marks 
      // (set such squares to CLEAR) 
      for (int i = 0; i < row; ++i) 
      { 
       for (int j = 0; j < col; ++j) 
       { 
        if (maze[i][j] == PATH || maze[i][j] == VISITED) 
        { 
          maze[i][j] = CLEAR; 
        } 
       } 
      } 
     } 

     public <ostream> ostream leftShift(ostream out, Maze m) 
     { 
      // X: wall 
      // C: creature 
      // : empty square 
      // .: path leading to the exit 

      // character on the square 
      char l; 
      for (int r = 0; r < m.row; ++r) 
      { 
       for (int c = 0; c < m.col; ++c) 
       { 
        switch (m.maze[r][c]) 
        { 
        case CLEAR: 
        case VISITED: 
         l = ' '; 
         break; 
        case WALL: 
         l = 'X'; 
         break; 
        case PATH: 
         l = '.'; 
         break; 
        } 
        // Check for the creature 
        if (r == m.creature.r && c == m.creature.c) 
        { 
         l = 'C'; 
         System.out.println(l); 
        } 

       } 

      } 
      return out; 
     } 




     public void markSquareAsVisited(Location l) 
     { 
      assert 0 <= l.r && l.r < row && 0 <= l.c && l.c < col; 
      //assert[][] maze != WALL; 

      maze[l.r][l.c] = VISITED; 
     } 

     public void markSquareAsPath(Location l) 
     { 
      assert 0 <= l.r && l.r < row && 0 <= l.c && l.c < col; 
      //assert[][] maze != WALL; 

      maze[l.r][l.c] = PATH; 

     } 

     public int square(Location l) 
     { 
      assert 0 <= l.r && l.r < row && 0 <= l.c && l.c < col; 
      return maze[l.r][l.c]; 
     } 

     public boolean insideMaze(Location l) 
     { 
      return (l.c >= 0 && l.c < col && l.r >= 0 && l.r < row); 
     } 

     public void findPath(Location l, Boolean success) 
     { 
      // This square is being visited 
      // Mark it as such 
      markSquareAsVisited(l); 

      // If it is the exit square, we are done 
      // Mark the square as part of the path 
      // set success to true 
      if (l == exit) 
      { 
       success = true; 
       markSquareAsPath(l); 
       return; 
      } 

      // try all possible moves from this square 
      // north, south, east and west 
      Location north = l; 
      north.r--; 
      Location south = l; 
      south.r++; 
      Location east = l; 
      east.c++; 
      Location west = l; 
      west.c--; 

      // If we haven't found the winning path yet and we can go north 
      // and we haven't been there already, look for the winning path 
      // starting from north 
      if (!success && insideMaze(north) && square(north) != WALL && square(north) != VISITED) 
      { 
       findPath(north,success); 
      } 
      // If we haven't found the winning path yet and we can go south 
      // and we haven't been there already, look for the winning path 
      // starting from south 
      if (!success && insideMaze(south) && square(south) != WALL && square(south) != VISITED) 
      { 
       findPath(south,success); 
      } 
      // If we haven't found the winning path yet and we can go east 
      // and we haven't been there already, look for the winning path 
      // starting from east 
      if (!success && insideMaze(east) && square(east) != WALL && square(east) != VISITED) 
      { 
       findPath(east,success); 
      } 
      // If we haven't found the winning path yet and we can go west 
      // and we haven't been there already, look for the winning path 
      // starting from west 
      if (!success && insideMaze(west) && square(west) != WALL && square(west) != VISITED) 
      { 
       findPath(west,success); 
      } 

      // If success is true, this location is part of a 
      // solution path. Mark the square as part of the path. 
      if (success) 
      { 
       markSquareAsPath(l); 
      } 
     } 

} 

這是我的位置類看起來像:

public class Location implements LocationInterface 
{ 
    // store a location in the maze 
    // r and c are the row and column of the location in the maze 
     public int r; 
     public int c; 

     // Comparison of 2 locations 
     public boolean equalsTo(Location l) 
     { 
      return (r == l.r && c == l.c); 
     } 

     public boolean notEqualsTo(Location l) 
     { 
      return (r != l.r || c != l.c); 
     } 

} 

回答

2

此行(在您的Maze課程中):

Scanner readFile = new Scanner (filename); 

不從文件中讀取文件名 - 它讀取文件名本身就好像它是輸入文件一樣。你反而想打開文件並閱讀它。嘗試改爲:

FileInputStream fis = new FileInputStream(filename); 
Scanner readFile = new Scanner (fis); 
// be sure to fis.close() later 
+0

雖然 – user1896464

+0

同樣錯誤在堆棧跟蹤中,查看確切的行號。它仍然是Maze.java:38?可能不會。查看哪條線路現在導致問題。 –

+0

不同的行,但代碼相同\t \t \t line 39:FileInputStream fis = new FileInputStream(filename); – user1896464