1

I am creating a Game of Life program that accepts user input patterns, using java.util.Scanner and java.io.File

The main issue is that I cannot seem to get the program to read the pattern.txt file...

I do not see any issue, the pattern.txt is in the same folder as the .java and .class files when I compile them.

Am I using File and Scanner correctly?

I've tried reordering the import statements, changing try & catch structure, and creating a new File(//..Path/../pattern.txt) to directly call the file via the relative path

1.txt is the pattern file:

20 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

`

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;

    public static void main(String [] args) throws FileNotFoundException{

    String inputFileName = "1.txt"; // Directly setting relative path

    int [][] initStates = getInitialStates(inputFileName);

    Grid gd = new Grid(initStates);

    gd.display();
    gd.getStats();
    gd.run();
    }

    public static int [][] getInitialStates(String fileName) {
    try {
        File file = new File(fileName);
    // checks to see if file was created
    //            if (file.exists())
    //                System.out.println("FILE EXISTS");
    //            else
    //                System.out.println("FILENOTFOUDN");
        Scanner input = new Scanner(file);     // Create scanner to read file
        int[][] states = null;
        int rows = input.nextInt();                   // get rows and cols values from first values of text file
        int cols = input.nextInt();
        // states = new int[rows][cols];           // Create 2d array w/ rows and cols
        states = new int[rows][cols];
    //        for (int i = 0; i < rows; i++) {
    //            states[i] = new int[cols];
    //        }

        // Read initial states from the input file
        for (int i = 0; i < states.length; i++) {
            for (int j = 0; j < states[0].length; j++) {
                states[i][j] = input.nextInt();
            }
        }
        return states;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
    //        return states;
}

`

Error output is FileNotFoundException printing the stack trace:

java.io.FileNotFoundException: 1.txt (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.util.Scanner.<init>(Scanner.java:639)
at client.getInitialStates(client.java:67)
at client.main(client.java:24)
Exception in thread "main" java.lang.NullPointerException
at Grid.<init>(Grid.java:14)
at client.main(client.java:26)

I get the NullPointerException because the getInitialStates() method isn't returning the 2d array because it cannot read the file.

Small_Kitten
  • 116
  • 9
  • "The main issue is that I cannot seem to get the program to read the pattern.txt file..." — do you get the `FileNotFoundException`, or the inability to read the file is manifested in some other way? – andrybak Mar 29 '19 at 21:32
  • Yes, it outputs the FileNotFoundException and a stack trace showing that it causes an error at the Scanner statement. – Small_Kitten Mar 29 '19 at 21:46
  • Then `FileNotFoundException` is the first issue you will need to solve. It would be better to include it in the question front and center. Working with `Scanner` should be secondary. – andrybak Mar 29 '19 at 22:48

1 Answers1

1

If as per comment a FileNotFoundException is thrown, it means that the working directory of your program (i.e. directory where java YourMainClassName is invoked) is different from location of the file you're trying to open by its name.

You can check the working directory:

System.out.println("Working Directory = " + System.getProperty("user.dir"));

As described in this answer: https://stackoverflow.com/a/7603444/1083697

andrybak
  • 2,129
  • 2
  • 20
  • 40
  • Awesome! That helped, it seems that IntelliJ needs the .txt file to be in the project folder not in the src folder. That's something to take note of I guess. Now the program runs and doesn't give FileNotFoundException, although it is not printing the grid.. I guess I have more debugging to do. – Small_Kitten Mar 30 '19 at 03:44
  • @Small_Kitten, also checkout run configuration settings. One of the options is to change working directory. – andrybak Apr 19 '19 at 17:49