title: java.util.NoSuchElementException: Read words from a file
body: <p>what`s wrong with this simple java program?</p>
  
  <p>I am trying to read words from a file and get the following exception:</p>
  
  <pre><code>complication
  dilemma
  lavender
  issue
  happy
  weird
  Exception in thread "main" java.util.NoSuchElementException
      at java.util.Scanner.throwFor(Scanner.java:907)
      at java.util.Scanner.next(Scanner.java:1416)
      at com.recrawl.util.ReadWordsTextfile.readWordsFromTextFile(ReadWordsTextfile.java:32)
      at com.recrawl.util.ReadWordsTextfile.main(ReadWordsTextfile.java:13)
  </code></pre>
  
  <p>As you can see I get the words however, I also get an exception. I am using the following code:</p>
  
  <pre><code>public static void main(String[] args) {
      String path = "Word_and_phrases";
      ReadWordsTextfile r = new ReadWordsTextfile();
      r.readWordsFromTextFile(path);  
  }
  
  public ArrayList&lt;String&gt; readWordsFromTextFile(String path) {
      ArrayList&lt;String&gt; resultList = new ArrayList&lt;String&gt;();
  
      FileInputStream fileIn = null;
      try {
          fileIn = new FileInputStream(path);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
      Scanner scan = new Scanner(fileIn);
      while(scan.hasNextLine()) {
          String word = scan.next();
          word = word.replace(",", "");
          System.out.println(word);
          resultList.add(word);
      }
      scan.close();
      System.out.println(resultList.toString());
      return resultList;
  }
  </code></pre>
  
  <p>I really appreciate your answers!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The content of the txt file looks like that:</p>
  
  <pre><code>complication, dilemma, lavender, issue, happy, weird
  </code></pre>
  
  <p>Line 32 ist that:</p>
  
  <pre><code>    String word = scan.next();
  </code></pre>