为什么我得到FileNotFoundException?

时间:2015-10-03 21:44:37

标签: java arrays 2d filenotfoundexception maze

我的Java代码编译和(最后,经过漫长的一周)看起来是正确的。但是,为什么我得到“FileNotFoundException”?这是我的代码:

import java.io.*;
import java.util.*;

public class Maze
{
private char[][] maze;
private String file;

private char exit = 'E';
private char wall = '+';
private char blankSpace = ' ';
private char openPath = 'O';
private char traveledPath = '*';
private char startingPoint = 'S';

private int numberOfRows; // number of rows in maze 2D array, as dictated by 1st line of incoming text file.
private int numberOfColumns; // number of columns in maze 2D array, as dictated by 2nd line of incoming text file.
private int startingPointRow; // starting point row in maze 2D array, as dictated by 3rd line of incoming text file.
private int startingPointColumn; // starting point column in maze 2D array, as dictated by 4th line of incoming text file.
private int row; // row in maze 2D array
private int column; // column in maze 2D array

private Scanner in;

public Maze(String fileName) throws IOException 
{
    file = fileName;
    File reader = new File(file);
    in = new Scanner(reader);

    // read in the first 4 lines as integers and assign them to variables
    while (in.hasNextInt())
    {
        numberOfRows = Integer.parseInt(in.nextLine()); // assign 1st line of text file as number of rows in maze array
        numberOfColumns = Integer.parseInt(in.nextLine()); // assign 2nd line of text file as number of columns in maze array

        startingPointRow = Integer.parseInt(in.nextLine()); // assign 3rd line of text file as starting point row in maze array
        startingPointColumn = Integer.parseInt(in.nextLine()); // assign 4th line of text file as starting point column in maze array
    }

    // read each line into the array
    while (in.hasNext())
    {
        String str = in.next();
        char[] oneDcharArray = str.toCharArray();

        for (row = 0; row <= numberOfRows; row++)
        {
            for (column = 0; column <= numberOfColumns; column++)
            {
                maze = new char[numberOfRows][numberOfColumns];
                maze[row][column] = oneDcharArray[column % numberOfColumns + row * numberOfColumns];
                printMazeRunnerPath();
            }
        }
    }
}

public void printMazeRunnerPath() // recursive method to print the maze runner's path through the maze
{
    //** directions of maze runner movement **
    //   maze[row--][column] = up
    //   maze[row++][column] = down
    //   maze[row][column--] = left
    //   maze[row][column++] = right

    if (maze[row][column] == wall) // If the file reader hits a '+', then print a blank space.
    {
        System.out.print(blankSpace);
        printMazeRunnerPath(); // recursive case
    }

    // Start at the starting point ('S'). Run the maze.
    if (maze[row][column] == startingPoint)
    {
        System.out.print(startingPoint);
        printMazeRunnerPath(); // recursive case
    }

    // If you go right and hit a wall, go to the next line.
    if (maze[row][column] == wall && maze[row][column++] == wall)
    {
        in.nextLine();
        printMazeRunnerPath(); // recursive case
    }

    // If you go down and hit an 'O', then go that path.
    if (maze[row][column] == openPath && maze[row++][column] == openPath)
    {
        System.out.print(traveledPath);
        printMazeRunnerPath(); // recursive case
    }

    // If you go up and hit a wall, then go down.
    if (maze[row][column] == wall && maze[row--][column] == wall)
    {
        System.out.print(blankSpace);
        // maze[row++][column];
        System.out.print(traveledPath);
        printMazeRunnerPath();
    }

    // If you go left and hit a wall, then go up and hit a wall, then go down and hit an 'O', take the path down.
    if (maze[row][column] == openPath && maze[row][column--] == wall && maze[row--][column] == wall && maze[row++][column] == openPath)
    {
        System.out.print(blankSpace);
        // maze[row++][column];
        System.out.print(traveledPath);
        printMazeRunnerPath();
    }

    // ....
    if (maze[row][column] == exit) // Terminating condition of the recursive method. If you hit an 'E', close the file reader.
    {
        in.close();
    }
}
}

这是我的主要方法:

import java.io.*;
public class MazeTester
{
        public static void main(String[] args)
        {
            try
            {
                Maze myMaze = new Maze("MazeTestFile.txt");
            }
            catch (IOException exception)
            {
                System.out.println(exception);
            }
        }
    }

以下只是MazeTestFile.txt的一部分:

35
35
0
10
++++++++++S++++++++++++++++++++++++
++++++++++OOOOOOOOOOOOOOOOOOOOOOOOO
++++++++++O++++++++++++++++++O+++++
OOOOOOOOOOOOOOOOOOOOOOOOOO+++O++OOE

1 个答案:

答案 0 :(得分:1)

我建议先删除你的'file'变量,而不是: File reader = new File(fileName);

然后,我会刷新eclipse项目。如果项目在eclipse中打开且文件不存在,那么它在eclipse中仍然不存在。转到层次结构视图中的项目,右键单击,然后单击刷新。确保右键单击java项目文件夹,而不是层次结构视图中的任何开放空间。

如果这不起作用,请尝试为您要阅读的文件提供确切的文件路径。 所以而不是:

Maze maze = new Maze("MazeTestFile.txt");

我愿意:

Maze maze = new Maze("C:/users/blah/documents/blah/MazeTestFile.txt");

由于这是java,另外需要注意的是使用正斜杠而不是标准反斜杠来表示文件夹更改。