Trying to input a text file but the program won't read it.
I have to open a text file named Grades.txt and everytime I try to open it, it gives me a FileNotFoundException. I am not sure how to overcome this and get my program to read the file.
Code Java:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class EchoFileData
{
public static void main(String [] args) throws IOException
{
int grade;
File inputFile = new File( "Grades.txt" );
Scanner scan = new Scanner( inputFile );
while ( scan.hasNext() )
{
grade = scan.nextInt();
System.out.println( grade );
}
}
}
Re: Trying to input a text file but the program won't read it.
Is the program looking in the right place for the file? Or is the file in the right place?
Print out the File object's absolutePath to see where the program is looking for the file.
Re: Trying to input a text file but the program won't read it.
I'm not sure how to do that. It is a download file that I downloaded for an assignment. That is where the text file is located. I tried moving it to my computer, but I'm not exactly sure where to move it to get it to open or how to print the file object's absolutePath.
Re: Trying to input a text file but the program won't read it.
Quote:
how to print the file object's absolutePath.
Try something like: System.out.println(inputFile.getabsolutepath());
Check the API doc for the File class to get the correct spelling for the method.
The printed out path will show you where the program is looking for the file.
Re: Trying to input a text file but the program won't read it.
Thank You so much! I finally figured it out!