Create a program via Java that has atleast 8 characters long, one upper case, an
ID: 3596996 • Letter: C
Question
Create a program via Java that has atleast 8 characters long, one upper case, and one lower case, and one digit.
here is my code:
public static void main(String[] args)
{
//variables defined
String passwords;
char password =0;
//settings up password condition to false
boolean passwordcondition= false;
boolean size=false;
boolean digit=false;
boolean upper=false;
boolean lower=false;
//inputting a scanner into the system
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a password: (Caution must have one uppercase, one lowercase, and a digit for password to be valid!)");
passwords=keyboard.nextLine();
while(!passwordcondition)
{
for (int p=0; p<passwords.length(); p++)
{
if(Character.isDigit(p))
{
digit = true;
}
else if(Character.isUpperCase(p))
{
upper = true;
}
else if(Character.isLowerCase(p))
{
lower = true;
}
else if(passwords.length() > 8)
{
size = true;
}
}
if(digit == true && upper == true && lower == true && size == true)
{
System.out.printf("You have entered a valid password");
System.exit(0);
}
else
{
System.out.println("You have entered an invalid password");
return;
}
}
}
my problem is I cannot get it back into the loop if I have an invalid answer.
Explanation / Answer
// I have edited the program as required
import java.util.Scanner;
public class pass
{
public static void main(String[] args)
{
//variables defined
String passwords;
char password =0;
//settings up password condition to false
boolean passwordcondition;
boolean size=false;
boolean digit=false;
boolean upper=false;
boolean lower=false;
//inputting a scanner into the system
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("Please enter a password: (Caution must have one uppercase, one lowercase, and a digit for password to be valid!)");
passwords=keyboard.nextLine();
char ch;
for (int p=0; p<passwords.length(); p++)
{
ch=passwords.charAt(p);
if(Character.isDigit(ch))
{
digit = true;
}
else if(Character.isUpperCase(ch))
{
upper = true;
}
else if(Character.isLowerCase(ch))
{
lower = true;
}
else if(passwords.length() >= 8)
{
size = true;
}
}
if(digit == true && upper == true && lower == true && size == true)
{
System.out.printf("You have entered a valid password");
passwordcondition=true;
System.exit(0);
}
else
{
System.out.println("You have entered an invalid password");
passwordcondition=false;
}
}while(passwordcondition==false);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.