Hi. I am new to Java and am trying to figure out a homework problem. It is a password verifier with the following criteria:
- at least 6 characters long
- conatin at least one uppercase and one lowercase letter
- should have at least one digit
This is what I have so far - but am not sure how to loop from letter to letter in the user's input.
import java.util.Scanner;
public class PasswordDemo
{
public static void main(String[] args)
{
String input; // To hold input
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get a password.
System.out.print("Enter a password: ");
input = keyboard.nextLine();
// Check the password.
if (!PasswordVerifier.isValid(input))
System.out.println("Invalid password.");
else
System.out.println("Valid password.");
}
}
import java.util.Scanner;
class PasswordVerifier
{
static String str;
public static isValid(String str)
{
//I am not sure what to put here so that this method works right to return the value to the main method.
}
private static boolean hasLength(String str)
{
boolean goodSoFar = true;
if (str.length() >=6)
goodSoFar = false;
System.out.println("The password is not long enough. Try again.");
return goodSoFar;
}
private static boolean hasUpperCase(String str)
{
boolean goodSoFar = true;
int i = 0;
while (goodSoFar && i < 6)
{
if (!Character.isUpperCase(str.charAt(i)))
goodSoFar = false;
System.out.println("The password must have at least one UpperCase letter. Try again.");
i++;
}
return goodSoFar;
}
private static boolean hasLowerCase(String str)
{
boolean goodSoFar = true;
int i = 0;
while (goodSoFar && i < 6)
{
if (!Character.isLowerCase(str.charAt(i)))
goodSoFar = false;
System.out.println("The password must have at least one lower case letter. Try again.");
i++;
}
return goodSoFar;
}
private static boolean hasDigit(String str)
{
boolean goodSoFar = true;
int i = 0;
while (goodSoFar && i < 6)
{
if (!Character.isDigit(str.charAt(i)))
goodSoFar = false;
System.out.println("The password must contain at least one number. Try again.");
i++;
}
return goodSoFar;
}
}
Any help would be appreciated.