为什么我得到FileNotFoundException?

时间:2021-09-29 22:48:03

My Java code compiles and (finally, after a long week) looks correct. But, why am I getting a "FileNotFoundException"?? Here is my code:

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

Here is my main method:

这是我的主要方法:

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

Below is just part of the MazeTestFile.txt:

以下是MazeTestFile.txt的一部分:

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

1 个解决方案

#1


1  

I recommend first to get rid of your 'file' variable and rather: File reader = new File(fileName);

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

Then, I would refresh the eclipse project. If the project was opened in eclipse with the file nonexistent, then it still doesn't exist in eclipse. Go to your project in the hierarchy view, right click, and hit refresh. Make sure you right click your java project folder, and not any open space in the hierarchy view.

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

If this doesn't work, attempt to give the exact file path for the file you wish to read. So instead of:

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

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

迷宫迷宫=新迷宫(“MazeTestFile.txt”);

I would do:

我会做:

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

迷宫迷宫=新迷宫(“C:/users/blah/documents/blah/MazeTestFile.txt”);

Since this is java, another thing to look out for is using forward slash instead of the standard backslash for signifying folder change.

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

#1


1  

I recommend first to get rid of your 'file' variable and rather: File reader = new File(fileName);

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

Then, I would refresh the eclipse project. If the project was opened in eclipse with the file nonexistent, then it still doesn't exist in eclipse. Go to your project in the hierarchy view, right click, and hit refresh. Make sure you right click your java project folder, and not any open space in the hierarchy view.

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

If this doesn't work, attempt to give the exact file path for the file you wish to read. So instead of:

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

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

迷宫迷宫=新迷宫(“MazeTestFile.txt”);

I would do:

我会做:

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

迷宫迷宫=新迷宫(“C:/users/blah/documents/blah/MazeTestFile.txt”);

Since this is java, another thing to look out for is using forward slash instead of the standard backslash for signifying folder change.

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