简体   繁体   中英

Why am I getting a FileNotFoundException?

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

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:

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

I recommend first to get rid of your 'file' variable and rather: 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.

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

I would do:

Maze maze = new Maze("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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM