I have an assignment for my CS class where it says to read a file with several test scores and asks me to sum and average them. While summing and averaging is easy, I am having problems with the file reading. The instructor said to use this syntax
Scanner scores = new Scanner(new File("scores.dat"));
However, this throws a FileNotFoundException
, but I have checked over and over again to see if the file exists in the current folder, and after that, I figured that it had to do something with the permissions. I changed the permissions for read and write for everyone, but it still did not work and it still keeps throwing the error. Does anyone have any idea why this may be occurring?
EDIT: It was actually pointing to a directory up, however, I have fixed that problem. Now file.exists()
returns true
, but when I try to put it in the Scanner
, it throws the FileNotFoundException
Here is all my code
import java.util.Scanner;
import java.io.*;
public class readInt{
public static void main(String args[]){
File file = new File("lines.txt");
System.out.println(file.exists());
Scanner scan = new Scanner(file);
}
}
new File(".")
– Klaxonnew File("scores.dat")
and double-check to see if it exists in the expected directory. – Orphaorphanls -la
from the directory where you're runningjava
. – Peelernew File("scores.dat").exists()
return? What doesnew File(".").listFiles()
returns? Do you find your file in the list? If you pick that instance, does it work with the scanner? – Klaxonnew File("scores.dat").getAbsolutePath()
and make sure the file's really in that location. – Ceramistnew File().getName()
instead ofnew File().getAbsolutePath()
, even thoughgetName()
returned the correct path, it did not includefile:
at the beginning of the String. – Barela