title: File does not exist? Exception in thread "main" java.io.FileNotFoundException: - Java
body: <p>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. </p>
  
  <p>Here is the beginning code I'm working with:</p>
  
  <pre><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 &lt; 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 &lt; 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;
  }
  </code></pre>
  
  <p>Here's the trace I'm getting if I use quotes:</p>
  
  <pre><code>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.&lt;init&gt;(FileInputStream.java:146)
      at java.util.Scanner.&lt;init&gt;(Scanner.java:656)
      at accountdriver.AccountDriver.readFile(AccountDriver.java:38)
      at accountdriver.AccountDriver.main(AccountDriver.java:25)
  Java Result: 1
  </code></pre>
  
  <p>And here's the trace without quotes:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>And here is the properly working code:</p>
  
  <pre><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 &lt; 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 &lt; 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;
  }
  </code></pre>
  
  <p>I just needed to add Line to the end of each fileInput.next when I was attempting to read in  strings.</p>