Q:

Using strings in Java to accept a password based on certain requirements

This program is suppose to accept input into a string, the password.  The program then checks to make sure that the password is at least 8 characters in length, contains at least 2 digits, and only contains letters and numbers.  I can't seem to get the correct way to count at least 2 digits, although from what i've seen this method should work.
This is the error I get
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 10
    at java.lang.String.charAt(String.java:658)
    at Password.main(Password.java:35)
import java.util.*;

public class Password{
    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    String s;
    int numbers = 0;

    System.out.println("Enter a password (Must contain only letters and numbers, ");
    System.out.print("minimum of 8 characters with atleast two numbers): ");    
    s = input.nextLine();

    while(1==1){
    if (s.length() < 8){
        System.out.println("Password is too short");
        System.out.println("Enter correctly formatted password");
        s = input.nextLine();
        continue;
        }

    if (s.matches("^[a-zA-Z0-9_]+$")){  
    }
    else{
        System.out.println("Password may only contain Letters and Digits");
        System.out.println("Enter correctly formatted password");
        s = input.nextLine();
        continue;
        }

    int i;
    for (i = 0; i <= s.length(); i++){
        if (Character.isDigit(s.charAt(i))){
            numbers++;
            }
            }

    if (numbers < 2){
        System.out.println("Password must contain atleast 2 digits");
        System.out.println("Enter correctly formatted password");
        s = input.nextLine();
        continue;
        }
        break;
        }

    while (0==0){   
    System.out.println("Reenter Password to see if it matches");
        String a = input.nextLine();

    if (s.equals(a)){
        System.out.println("Password matches!");
        break;
        }
    else{
        System.out.println("Password does not match");
        continue;
    }
    }
    }
    }

A:

In your for loop, you should not allow i to equal s.length()
for (i = 0; i <= s.length(); i++)

ArrayIndexOutOfBoundsException is caused when you try to access s[s.length()].

