title: How to get class variable file check to print line rather than throw exception
body: <p>I'm trying to get a method to check for the existence of a file, and print a message that the file doesn't exist, but it also has to be a class variable rather than an instance variable.</p>
  <p>I had it working when it was only in subString method, was not a class variable and without infix/suffix/prefix code.</p>
  <p>Here is my code.  It's still a little bit messy and no conforming to formatting convention.</p>
  <p>Appreciate your help.</p>
  <pre><code>package wordgames;
  
  import java.io.File;
  import java.util.Scanner;
  import java.io.FileInputStream;
  import java.io.FileReader;
  import java.io.FileNotFoundException;
  import java.util.InputMismatchException;
  
  public class WordGames {
  
      private static final String DICTIONARY = &quot;dictionary.txt&quot;;
      private static String[] wordsCollection;
      private static int wordCount = 0;
  
      public static void main(String[] args) throws Exception {
          wordsCollection = new String[100];
          File fileReader = new File(DICTIONARY);
          Scanner fileScanner = new Scanner(fileReader);
  
          while (fileScanner.hasNextLine()) {
              String line = fileScanner.nextLine();
              wordsCollection[wordCount] = line;
              wordCount++;
          }
  
          getSelection();
      }
  
      static String getSelection() throws FileNotFoundException {
          Scanner keyboardInput = new Scanner(System.in);
          System.out.println(&quot;Welcome to the Word Games program menu.&quot;);
          System.out.println(&quot;Select from one of the following options.&quot;);
          System.out.println(&quot;1. Substring problem.&quot;);
          System.out.println(&quot;2. Points problem.&quot;);
          System.out.println(&quot;3. Exit.&quot;);
          System.out.println(&quot;Enter your selections: &quot;);
          String selection = keyboardInput.next();
  
          switch (selection) {
  
          case &quot;1&quot;:
              subStringProblem();
          case &quot;2&quot;:
              pointsProblem();
          case &quot;3&quot;:
              System.out.println(&quot;Good Bye!&quot;);
              System.exit(0);
          default:
              System.out.println(&quot;Invalid option.  Try again.&quot;);
              getSelection();
          }
  
          return null;
      }
  
      static void subStringProblem() throws FileNotFoundException {
          File fileReader = new File(&quot;DICTIONARY.txt&quot;);
          Scanner fileScanner = new Scanner(fileReader);
          if (fileReader.isFile() == true) {
          } else {
              System.out.println(&quot;File doesn't exist.  Exiting.&quot;);
              System.exit(0);
          }
          System.out.println(&quot;Substring problem.&quot;);
          System.out.println(&quot;Enter a Substring:&quot;);
          Scanner keyboardInput = new Scanner(System.in);
          String subString = keyboardInput.next();
          System.out.print(subString);
          System.out.println();
          String notFound = &quot; - not found&quot;;
          String infixFound = &quot; - infix&quot;;
          String prefixFound = &quot; - prefix&quot;;
          String suffixFound = &quot; - suffix&quot;;
  
          for (int i = 0; i &lt; wordCount; i++) {
              String temp = wordsCollection[i];
  
              boolean found = false;
  
              if (wordsCollection[i].startsWith(subString)) {
                  found = true;
                  temp = temp + prefixFound;
              }
  
              if (wordsCollection[i].endsWith(subString)) {
                  found = true;
                  temp = temp + suffixFound;
              }
  
              if (wordsCollection[i].contains(subString)) {
                  found = true;
                  temp = temp + infixFound;
              }
  
              if (!found) {
                  System.out.printf(&quot; &quot; + wordsCollection[i] + notFound + &quot;\n&quot;);
              } else {
                  System.out.printf(&quot; &quot; + temp + &quot;\n&quot;);
              }
  
          }
  
          getSelection();
      }
  
      private static void pointsProblem() throws FileNotFoundException {
          System.out.println(&quot;Points problem.&quot;);
          getSelection();
      }
  }
      
  </code></pre>