My professor assigned a program that opens a file and reads its lines. He provided the following example:
import java.io.*;
import java.util.Scanner;
public class LineNumbers {
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the filename: ");
String filename = keyboard.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext()) {
String familyName = inputFile.nextLine();
System.out.println(familyName);
}
inputFile.close();
keyboard.close();
}
}
The issue is, when I run the program and it prompts me to enter a filename, what exactly should I type? If I create a text file named "test.txt"
on my desktop and enter "test.txt"
into the program, it doesn’t open the file.
Am I missing a specific format or path to ensure the file is correctly found and read?