title: How would I set a string variable and pass it into a File reader scanner as a file name?
body: <p>I am writing a Cipher program which asks the user for a file name, stores the name as a variable, and is then supposed to read the file based on the String variable. It compiles, but I keep getting an IOException when I try to run it. </p>
  
  <pre><code> java.io.FileNotFoundException:  (No such file or directory)
   at java.io.FileInputStream.open(Native Method)
   at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:146)
   at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:101)
   at java.io.FileReader.&lt;init&gt;(FileReader.java:58)
   at Cipher.main(Cipher.java:22)
  </code></pre>
  
  <p>I don't understand what is happening. It gives the error before the user has a change to type the file name. Here is my code:</p>
  
  <pre><code>import java.util.Scanner;
  import java.io.*;
  
  public class Cipher {
  
  public static void main (String [] args) throws FileNotFoundException {
  
          String inFile = "";
  
          Scanner sc = new Scanner (System.in);
              System.out.println("Welcome to Caeser Cipher");
              System.out.println("Enter 1 to encipher, or 2 to decipher (-1 to exit): ");
              int cipher = sc.nextInt();
  
          System.out.println("What non-negative shift should I use?");
              int shift = sc.nextInt();
  
              System.out.println("What is the input file name?");
              inFile = sc.nextLine();
              try {
  
                  Scanner input = new Scanner (new FileReader (inFile) ) ;
                  String line = input.nextLine();
  
  
  
         /* System.out.println("What is the output file name?");
           String outFile = sc.nextLine();*/
  
          Scanner input = new Scanner (new FileReader (inFile) ) ;
          input.useDelimiter("['.!?0-9+");
  
          String line = input.nextLine();
  
          while (input.hasNextLine()) {
  
                  line = input.nextLine();
  
                  if (cipher == 1) {
                      System.out.println(caeserEncipher(line, shift));
                  } else if (cipher == 2) {
                      System.out.println(caeserDecipher(line, shift));
                  } else {
                      System.out.println ("Enter 1 to encipher, or 2 to decipher (-1 to exit)");
                      return;
                  }
              }
          }
  
          catch (FileNotFoundException e) {
              System.out.println ("Trouble opening or reading the file...");
              System.out.println ("Perhaps it was misspelled!");
              e.printStackTrace();
          }
      }
  
     public static String caeserEncipher(String input, int shift) {
          int arr[] = new int[input.length()];
          StringBuilder builder = new StringBuilder();
          int length = input.length();
          String output = "";
  
          for (int i = 0; i &lt; length; i++) {
              arr[i] = (int) input.charAt(i);
              arr[i] += shift;
          }
  
          for (int i = 0; i &lt; length; i++) {
              builder.append((char)arr[i]);
          }
  
          output = builder.toString();
          return output;
  
  }
  
  public static String caeserDecipher(String input, int shift) {
  int arr[] = new int[input.length()];
  StringBuilder builder = new StringBuilder();
  int length = input.length();
  String output = "";
  
  for (int i = 0; i &lt; length; i++) {
      arr[i] = (int) input.charAt(i);
      arr[i] -= shift;
  }
  
  for (int i = 0; i &lt; length; i++) {
      builder.append((char)arr[i]);
  }
  
  output = builder.toString();
  return output;
  
      }
  
  }
  </code></pre>