Hey guys
I have to write this program for AP Computer Science. It involves writing a main where it brings in a txt file with values, and reads all of them. Eventually, I have to modify it to read the values in to an array. Right now, I can't get it to read the file. My teacher gave us a code snippet to use that will help read the file.
This is my version of the snippet:

import java.util.Scanner;
import java.io.*;
public class main
{
    public static void main(String [] args) throws FileNotFoundException
    {
        File aFile = new File("C:\\Users\\John\\Documents\\values.txt");
        Scanner scan = new Scanner(new FileReader(aFile));
        
        int value = 0;
        while (scan.hasNext() && value !=999)
        {
            value=scan.nextInt();
        }
        scan.close();
    }
}

It compiles, but when I run it, it gives an error on the line with Scanner scan...
Any ideas?

Recommended Answers

All 6 Replies

You'll find that "an error" is very little to work with. Post the entire error message.

Where it says John in the code, it should be Jared. Spell check switched it.

This is the error:
java.io.FileNotFoundException: C:\Users\Jared\Documents\values.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileReader.<init>(FileReader.java:72)
at main.main(main.java:15)

And what do you think that error means?

It OBVIOUSLY can't find the file although it IS in that directory.

Check spelling and capitalization as well.

Careful of file extensions, too. Windows will mess you up sometimes... if you save something with the extension, make sure it saves as values.txt and not (say) values.txt.java (this shouldn't happen with any IDE I'm familiar with, but you never did say how you created the file).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.