This site requires JavaScript, please enable it in your browser!
Greenfoot back
TheGoldenProof
TheGoldenProof wrote ...

2018/9/3

Reading digits of pi from a .txt file

TheGoldenProof TheGoldenProof

2018/9/3

#
I want to make a thing that visualizes pi and I have a .txt file with the digits in it. Where in the project folder do I put the file and how to I read the digits? From looking at other discussions for reading text files I notice that it uses exception handling, which is something I know nothing about. I'll try to look into it but there's a chance that by the time I get a response, I still wont understand it.
danpost danpost

2018/9/3

#
TheGoldenProof wrote...
I want to make a thing that visualizes pi and I have a .txt file with the digits in it. Where in the project folder do I put the file and how to I read the digits?
It goes directly in your project file (where the .JAVA, .CLASS and .CTXT files are located). If you make an executable jar file out of the project, however, it will need to be alongside with the jar file (not in it), preferably in a folder containing both of them together, unless you include the jar directory in the path given for the file in your code before creating the jar file). That should not be a problem since you are only reading the file -- not writing to it.
From looking at other discussions for reading text files I notice that it uses exception handling, which is something I know nothing about. I'll try to look into it but there's a chance that by the time I get a response, I still wont understand it.
Exception handling is not so difficult and you can read up on it here in the Java tutorials.
TheGoldenProof TheGoldenProof

2018/9/3

#
Thanks! I looked into exception handling and it is pretty easy. Right now I have this for getting the file and breaking it into characters and it works perfectly.
Scanner input;
    char[] digits = new char[1000000];
    
    int i = 0;
    public Bkgd()
    {    
        super(1000, 1000, 1);
        try {
            input = new Scanner(new File("pi1000000.txt"));
            input.useDelimiter("");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        while (i<digits.length) {
            digits[i] = input.next().charAt(0);
            i++;
        }
    }
I don't understand what you mean by "unless you include the jar directory in the path given for the file in your code before creating the jar file." Could I put the file in the images/sounds folder and change the pathname to include the text file in the jar file?
danpost danpost

2018/9/3

#
TheGoldenProof wrote...
I don't understand what you mean by "unless you include the jar directory in the path given for the file in your code before creating the jar file." Could I put the file in the images/sounds folder and change the pathname to include the text file in the jar file?
I was just trying to point out that the root directory is different when as an executable jar file. The root is does not include the jar folder and therefore it would need to be included in your code if you were to have the txt file inside the jar folder. Whereas, when opened in greenfoot, the root includes the project folder.
You need to login to post a reply.