Question

I'm working on a project for class and I'm attempting to create a method that will read a file and fill an array. Inside the method I'm also using another method called from a class I've created. I prompt the user to enter the file to be read and no matter what I seem to do I keep getting an error that the file does not exist.

Here is the beginning code I'm working with:

 public static void main(String[] args) throws Exception {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the file to read from:");
    String readFile = input.nextLine();
    System.out.print("Enter the number of accounts in the file: ");
    int size = input.nextInt();
    Account[] Accounts = readFile(readFile, size);
    Account[] invalidAccounts = new Account[34];
    for (int i = 0; i < Accounts.length; i++) {
        String password = Accounts[i].getPassword();
        if (isValidPassword(password) == false) {
            invalidAccounts[i] = Accounts[i];
        }
    }
    createDistributionList(invalidAccounts);
}
public static Account[] readFile(String readFile, int size) throws Exception {
    Account[] Accounts = new Account[size];
    File myFile = new File(readFile);
    Scanner fileInput = new Scanner(myFile);
    for (int i = 0; i < Accounts.length; i++) {
            int stuID = fileInput.nextInt();
            String name = fileInput.next();
            String username = fileInput.next();
            String password = fileInput.next();
            Accounts[i] = new Account(stuID, name, username, password);
    }
    return Accounts;
}

Here's the trace I'm getting if I use quotes:

Enter the file to read from:"studentdata.txt"
Enter the number of accounts in the file: 34
Exception in thread "main" java.io.FileNotFoundException: "studentdata.txt" (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.util.Scanner.<init>(Scanner.java:656)
    at accountdriver.AccountDriver.readFile(AccountDriver.java:38)
    at accountdriver.AccountDriver.main(AccountDriver.java:25)
Java Result: 1

And here's the trace without quotes:

Enter the file to read from:studentdata.txt
Enter the number of accounts in the file: 34
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at accountdriver.AccountDriver.readFile(AccountDriver.java:40)
    at accountdriver.AccountDriver.main(AccountDriver.java:25)
Java Result: 1
BUILD SUCCESSFUL (total time: 7 seconds)

And here is the properly working code:

public static void main(String[] args) throws Exception {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the file to read from:");
    String readFile = input.nextLine();
    System.out.print("Enter the number of accounts in the file: ");
    int size = input.nextInt();
    Account[] Accounts = readFile(readFile, size);
    Account[] invalidAccounts = new Account[34];
    for (int i = 0; i < Accounts.length; i++) {
        String password = Accounts[i].getPassword();
        if (isValidPassword(password) == false) {
            invalidAccounts[i] = Accounts[i];
        }
    }
    createDistributionList(invalidAccounts);
}
public static Account[] readFile(String readFile, int size) throws Exception {
    Account[] Accounts = new Account[size];
    File myFile = new File(readFile);
    Scanner fileInput = new Scanner(myFile);
    for (int i = 0; i < Accounts.length; i++) {
            int stuID = fileInput.nextInt();
            String name = fileInput.nextLine();
            String username = fileInput.nextLine();
            String password = fileInput.nextLine();
            Accounts[i] = new Account(stuID, name, username, password);
    }
    return Accounts;
}

I just needed to add Line to the end of each fileInput.next when I was attempting to read in strings.

Was it helpful?

Solution

The stacktrace contains

"The filename, directory name, or volume label syntax is incorrect"

suggesting there are special characters in the name. It looks like you are putting " quotation marks around the name.

Enter the file to read from:"studentdata.txt"

You would use quotation marks if the filename was a Java string in the source code, but when inputting text via the console, these are not required.

OTHER TIPS

FileNotFoundException will either mean your file (name) doesn't exist, or the file is in the wrong location.

If you're going to read the file path as "studentdata.txt", what you need to understand is that, if working from an IDE, normally it tries to look for the file in the current working directory, which is the project root. So your file would need to be there.

You may instead just want to use the absolute path instead like C:/path/to/file.txt

In cases where the file will be an embedded resources, you may want to use MyClass.class.getResourceAsStream(fileName) where the file would be in the class path. So you could do

InputStream is = MyClass.class.getResourceAsStream(fileName);
Scanner scanner = new Scanner(is);

Where your file would be in the same package as your class, if you're just going to use the file name as the path.

So you have some things to consider.

You need to copy and paste the text file "studentdata.txt" into the project file(navigate to the specific project name)inside the location of NetbeansProjects. t should work just fine!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top