String index out of range
843785Oct 20 2008 — edited Oct 21 2008I've got this code written so far for a password validating project, and I've tested it by commenting out different parts, and I only get the string index out of range error when I uncomment this part:
boolean isOK = nice.meetsRequirements(unityID, oldPassword,
newPassword);
System.out.println(isOK);
but here is the whole thing:
import java.util.Scanner;
import java.lang.Character;
public class PasswordValidator
{
public PasswordValidator(){}
public boolean areEqual(String newPassword, String //***************compiler says the error is here
newPassword1)
{
if(newPassword.equals(newPassword1))
{
return true;
}
else
{
return false;
}}
public boolean meetsRequirements(String unityID, String
oldPassword, String newPassword)
{
char ch;
boolean blank = false;
for(int i=0; i<=newPassword.length(); i++)
{
ch = newPassword.charAt(i);
blank = Character.isWhitespace(ch);
}
if(blank == true)
return false;
else
return true;
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter your Unity ID: ");
String unityID = scan.nextLine();
System.out.println("Type in your old password: ");
String oldPassword = scan.nextLine();
System.out.println("Enter a new password: ");
String newPassword = scan.nextLine();
System.out.println("Please enter the new password again: ");
String newPassword1 = scan.nextLine();
PasswordValidator nice = new PasswordValidator();
boolean areThey = nice.areEqual(newPassword, newPassword1);
if(areThey == true)
{System.out.println("I need to change this at some point.");}
else
{
while(areThey == false)
{
System.out.println("Passwords do not match, please enter
again");
System.out.println("Enter a new password: ");
newPassword = scan.nextLine();
System.out.println("Please enter the new password again: ");
newPassword1 = scan.nextLine();
areThey = nice.areEqual(newPassword, newPassword1);
if(areThey == true)
continue;
}
}
boolean isOK = nice.meetsRequirements(unityID, oldPassword,
newPassword);
System.out.println(isOK);
}
}