title: Java trouble checking if password string is valid
body: <p>So I'm trying to get this to take user input as a string and check the password to make sure of these two things:</p>
  
  <ol>
  <li>The password is a minimum of 8 characters.</li>
  <li>The password only contains letters and numbers.</li>
  </ol>
  
  <p>Now the problem is this:
  Checking the password for a minimum of 8 characters works, but checking it to make sure it only contains letters and numbers does not work. It simply terminates without giving a single message if the minimum amount of numbers/letters are entered. <strong>However if it sees a character that is not a letter or number, it will print out this</strong>:</p>
  
  <p>Please enter a password: ###</p>
  
  <p>Password can only contain letters and numbers.</p>
  
  <p>Password can only contain letters and numbers.</p>
  
  <p>Password can only contain letters and numbers.</p>
  
  <p>Password accepted!</p>
  
  <p><strong>What it should output is this:</strong> </p>
  
  <p>Please enter a password: ###</p>
  
  <p>Password can only contain letters and numbers.</p>
  
  <p>or </p>
  
  <p>Please enter a password: test1234</p>
  
  <p>Password accepted!</p>
  
  <pre><code>    password.java    
      package Password;
      import java.util.Scanner;
  
      public class Password {
  
      public static void main(String[]args)
      {
  
      Scanner input = new Scanner (System.in);
      boolean valid = true;
      System.out.println("Please enter a password:");
      String password = input.nextLine();
      int i = 0;
      //declares i as the counter varible to control the loop and initializes it to 0
  
      if((password.length( ) &lt; 8 )) //check the passwords length and make sure it's a minimum of 8 characters
      {
      System.out.println("Password must have at least 8 characters.");
      valid = false;
      }
      //loops the code below it for the length of i until the password length is reached
      while(i &lt; password.length())
      {
      if ((password.charAt(i)&gt;='a' &amp;&amp; password.charAt(i)&lt;='z') ||  (password.charAt(i)&gt;='A' &amp;&amp; password.charAt(i)&lt;='Z') ||(password.charAt(i)&gt;='0' &amp;&amp; password.charAt(i)&lt;='9'))
      //loop through all the characters in the string entered and make sure they only consist of letters and numbers
         valid = true;
      else
      {
          System.out.println("Password can only contain letters and numbers.");  
          valid = false;
      }
      i++;
      //add an iteration to the loop
      }
  
      if(!valid == true)
      System.out.println("Password accepted!");
      }
      }
  </code></pre>
  
  <p>Any help at all with this would be great.</p>