title: Hangman assignment and loop conditions
body: <p>I have a JAVA assignment where I have to create a <code>Hangman</code> program using arrays and loops. </p>
  
  <ul>
  <li>user1 inputs a valid word (no numbers or symbols)</li>
  <li>user2 can try to guess the whole word in one shot OR guess using one letter in a total of 10 tries. Initially, user2 had to press 1 to guess the word or 2 to choose a letter. I changed that since I think this is more user friendly.</li>
  <li>user2 can try to guess the word at any point in time.</li>
  </ul>
  
  <p>The program needs to check that user2's input is valid</p>
  
  <ul>
  <li>Has to be an alphabet character, not a symbol</li>
  <li>Has to be only 1 character length OR the same length as the word to guess). </li>
  <li>An alphabet character cannot be used twice</li>
  </ul>
  
  <p>If the user2's input is invalid (conditions above), it gives an error message and asks user2 to input something else. Any invalid input does not count towards the 10 tries.</p>
  
  <p>For now, if the input is invalid (first 2 conditions above), the code behaves like it should. It gives an appropriate error message and the number of attempts does not increase.</p>
  
  <p>However, I cannot seem to be able to code a condition where if a letter is already chosen, that it also gives an error message and asks for another letter. </p>
  
  <p>I tries to put in an if condition <code>(if upperAlphabet[index] == '*', System.out.println("Duplicate. Try again"))</code> in the first do/while loop but it is not working properly: it increases the attempts count.</p>
  
  <p>I have the impression I have to do a for loop somewhere. Can't find where and how.</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.regex.Pattern;
  
  public class Test {
  
  public static void main(String[] args) {
  
      char[] upperAlphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
              'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
              'V', 'W', 'X', 'Y', 'Z' };  // Alphabet array to display to user2. 
  
      String wordtoGuess;
      char letterChoice;
      String userChoiceString;
      String wordArraytoString;
  
      do {
          System.out.println("Please enter a valid word (letters only)");   // Asks user1 for a valid word
          Scanner wordInput = new Scanner(System.in);
          wordtoGuess = wordInput.next();
          wordtoGuess = wordtoGuess.toUpperCase();
      } while (Pattern.matches("[A-Z]+", wordtoGuess) == false);     // Checks word is valid
  
      char[] wordArray = wordtoGuess.toCharArray();   // Puts word in character array               
      char[] guessingWordArray = new char[wordtoGuess.length()];
      for (int h = 0; h &lt; guessingWordArray.length; h++) 
          guessingWordArray[h] = '*'; // Displays the word to guess with * for user2
  
      for (int i = 0; i &lt; 20; i++) {  // Prints 20 empty lines to hide the input of the word from user1 
          System.out.println();
      }
  
      for (int j = 0; j &lt; 10; j++) {   // 10 attempts loop
  
          do {
  
              System.out.print("Word to guess: ");
              System.out.println(guessingWordArray);
              System.out
                      .println("Please choose a letter or solve the word.    " // Asks for a letter or the whole word
                              + "Attempts left: " + (10 - j));
              System.out.println(upperAlphabet);
              Scanner userInput = new Scanner(System.in);
              userChoiceString = userInput.next();
              userChoiceString = userChoiceString.toUpperCase();    // Captures the input as a string
              letterChoice = userChoiceString.charAt(0);
              letterChoice = Character.toUpperCase(letterChoice);  // Captures the first letter of the input
  
              if (Character.isLetter(letterChoice) == false) // Error if input is an alphabet letter
                  System.out.println("Invalid letter. Please try again.");
              if (userChoiceString.length() &gt; 1 // Error if input is not the same length as the whole word but more than 1 character
                      &amp;&amp; userChoiceString.length() &lt; wordtoGuess.length())
                  System.out.println(("Choose only one letter. Try again."));
  
          } while (userChoiceString.length() != 1
                  &amp;&amp; userChoiceString.length() != wordtoGuess.length()
                  || Character.isLetter(letterChoice) == false);
  
          if (userChoiceString.length() == 1) { // if input is only 1 character
  
              for (int k = 0; k &lt; upperAlphabet.length; k++) { // A used letter is replaced by * in alphabet array.
                  if (letterChoice == upperAlphabet[k]) {
                      upperAlphabet[k] = '*';
                  }
              }
  
              for (int m = 0; m &lt; wordtoGuess.length(); m++) { // If a letter is correct, reveal the correct letter in the word to guess.
                  if (letterChoice == wordArray[m]) {
                      guessingWordArray[m] = wordArray[m];
                  }
              }
              wordArraytoString = new String(guessingWordArray); // If all letters are revealed in the word to guess, display winning message when count of guesses.
              if (wordArraytoString.equals(wordtoGuess)) {
  
                  System.out.println(guessingWordArray);
                  System.out.print("Congratulations.");
                  System.out.print("You guessed the word: ");
                  System.out.print(wordtoGuess);
                  System.out.println(" in " + (j + 1) + " guesses.");
                  break;
  
              }
  
          } else if (userChoiceString.length() == wordtoGuess.length()) { // If user2 tries to guess the whole word, displays winning message and number of guesses
              if (userChoiceString.equals(wordtoGuess)) {
                  System.out.println(guessingWordArray);
                  System.out.print("Congratulations.");
                  System.out.print("You guessed the word: ");
                  System.out.print(wordtoGuess);
                  if (j == 0)
                      System.out.println(" in " + (j + 1) + " guess.");
                  else
                      System.out.println(" in " + (j + 1) + " guesses.");
                  break;
              } else
                  System.out.println("Wrong guess. Please try again."); // If guessing word is wrong.
          }
  
          if (j &gt;= 9)
              System.out
                      .println("You did not guess the word in the number of attemps allowed. Better luck next time."); // If exceeds 10 tries.
      }
  
  }
  
  }
  </code></pre>