Click to See Complete Forum and Search --> : I'm getting a FileNotFoundException error...


Psytherium
November 19th, 2004, 10:40 AM
I'm using scanner to read from a txt file, and I have the following:

Scanner scan = new Scanner(new File("grades.txt"));

and I'm getting this error:

unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown.

I also tried putting grades.txt at c:/grades.txt and did new File("c:/grades.txt") but that still gave me the same error. Any ideas?

cma
November 19th, 2004, 11:05 AM
That just means you have to handle an exception, put that code in a try/catch block:

try
{ Scanner scan = new Scanner(new File("grades.txt"));
.........
}
catch (FileNotFoundException e) { e.printStackTrace(); }

or the alternative is to declare the method to throw an exception where that code is in:

void read() throws FileNotFoundException
{ Scanner scan = new Scanner(new File("grades.txt"));
.....
}

Psytherium
November 19th, 2004, 12:59 PM
Thanks!