title: Java - Reading/Scanning a text file for string length and using it in the program
body: <p>I am trying to make a simple game of hangman in Java. I do have a text file named dictionary.txt containing 120K words from the English dictionary. The problem arises when I am going to prompt the user for a word length and displaying number of words with that particular length. </p>
  
  <p>After spending a fair amount of time here and googling I have gotten this far but now I am stuck:</p>
  
  <pre><code>import java.util.Scanner;
  import java.io.*;
  import java.io.IOException;
  
  
  
  public class Hangman 
  
  {
  
  
  public static void main(String[] args) throws IOException
  {
      // declaring variables
      int wordLength;
      int guessNumber;
  
  
      // initiate the scanner
      Scanner keyboard = new Scanner( System.in );
  
      // read the dictionary file 
  
      File file = new File("dictionary.txt");
      StringBuilder contents = new StringBuilder();
      BufferedReader reader = null;
  
  
  
      // prompt the user for word length
  
      System.out.println("Welcome to Hangman. Let's play! ");
      System.out.println("Please enter the desired word length: ");
      wordLength = keyboard.nextInt();
  
      while(wordLength &lt; 0 || wordLength &gt; 26)
      {
          System.out.println("This is not a valid word length. ");
          System.out.println("Please enter the desired word length: ");
          wordLength = keyboard.nextInt();
      }
  
      // prompt the user for number of guesses 
  
      System.out.println("How many guesses do you want to have? ");
      guessNumber = keyboard.nextInt();
  
      while(guessNumber &lt; 0)
      {
          System.out.println("Number of guesses has to be a postive integer. ");
          System.out.println("Please enter the desired number of guesses: ");
          guessNumber = keyboard.nextInt();
      }
  
  
  
  
      }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>My goal is to prompt the user for a word length and if the desired word length does not exist in the dictionary.txt file then it keeps asking until a valid response is given.</strong></p>
  
  <p><strong>I would also like to be able to prints how many words have a given word length (e.g if user types in "10", then it displays how many words in dictionary.txt have the length of 10 letters.</strong></p>
  
  <p>The following part of the code is the one I hope to replace with code that reads the txt file and acts thereafter:</p>
  
  <pre><code>while(wordLength &lt; 0 || wordLength &gt; 26)
  {
      System.out.println("This is not a valid word length. ");
      System.out.println("Please enter the desired word length: ");
      wordLength = keyboard.nextInt();
  }
  </code></pre>
  
  <p>It is possible that I have taken the wrong approach, so all feedback is very welcome!</p>