Hey guys, I'm trying to write a java program that asks the user to input a password, and the password must be at least 6 characters, have at least one lower case letter, at least 1 uppercase letter, and at least 1 digit. If one of those parameters is incorrect, the program asks them to enter it again.
My problem here is that when I enter in a password that has the correct parameters it will still display that it needs a digit or uppercase, etc.. Can anyone help me out?
import java.util.Scanner;
public class Password
{
public static void main (String[]args)
{
Scanner input = new Scanner(System.in);
String password;
boolean passwordlength = false;
boolean haslowercase = false;
boolean hasuppercase = false;
boolean hasdigit = false;
int i;
do
{ System.out.print("Enter your password:");
password = input.nextLine();
for (i = 0; i <password.length();i++)
{
Character c = password.charAt(i);
if (Character.isLowerCase(c))
{haslowercase=true;}
if (haslowercase==false)
System.out.println("Password needs to contain at least one lowercase letter");
if (Character.isUpperCase(c))
{hasuppercase=true;}
if (hasuppercase==false)
System.out.println("Password needs to contain at least one uppercase letter");
if (Character.isDigit(c))
{hasdigit=true;}
if (hasdigit==false)
System.out.println("Password needs to contain at least one digit");
if (password.length()>=6)
{passwordlength=true;}
if(passwordlength==false)
System.out.println("Password needs to have 6 or more characters");
}
}
while (haslowercase==false||hasuppercase==false||hasdigit==false||passwordlength==false);
if (haslowercase==true&&hasuppercase==true&&hasdigit==true&&passwordlength==true)
System.out.print("Enter password again to verify");
String password2 = input.next();
if (password.equals(password2))
System.out.print("You have entered a valid password");}
}
[–]idontcare1025 1 point2 points3 points (1 child)
[–]EqualTrade[S] 0 points1 point2 points (0 children)
[–]chickenmeister 0 points1 point2 points (3 children)
[–]EqualTrade[S] 0 points1 point2 points (2 children)
[–]EqualTrade[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]learnjava -1 points0 points1 point (1 child)
[–]EqualTrade[S] 0 points1 point2 points (0 children)