I am attempting to write a java program that acts as a \"Password\" tester. It n
ID: 3690370 • Letter: I
Question
I am attempting to write a java program that acts as a "Password" tester. It needs to test for at least 2 uppercase, 2 lower case, 2 digits, and 2 speical characters. It also needs to stop the program and ask for a new password to start over if any of the checks fail. The code below is the base of what I have and can get to work, I was wondering on how to make a second method to run another if loop to use the count and make it true or false to possibly repeat.
import java.util.Scanner;
public class Password_checker
{
public static void main (String [] arg)
{
Scanner in = new Scanner(System.in);
System.out.println("This program will allow you to enter a new password");
System.out.println("Your password must include 2 uppercase, 2 lowercase, 2 numbers, and 2 special characters.");
System.out.println("Please enter in a Password: ");
String test = in.next();
int y = test.length();
//gets limit length for for loop
for (int x=0; x < y; x++)
{
if (Character.isUpperCase(test.charAt(x)))
//tests characters at x position in string
{
int count=0;
count++;
}
}
for (int b=0; b < y; b++)
{
if (Character.isLowerCase(test.charAt(b)))
//tests characters at x position in string
{
int count=0;
count++;
}
}
for (int a=0; a<y; a++)
{
if (Character.isDigit(test.charAt(a)))
//tests characters at x position in string
{
int count=0;
count++;
}
}
}
}
Explanation / Answer
Solution:
package com.nancy.chegg.qanda;
import java.util.Scanner;
public class Password_checker {
public static void main(String[] arg) {
Scanner in = new Scanner(System.in);
System.out
.println("This program will allow you to enter a new password");
System.out.println(
"Your password must include 2 uppercase, 2 lowercase, 2 numbers, and 2 special characters.");
boolean isValid = false;
do {
int len = 0;
int upperCount = 0;
int lowerCount = 0;
int digCount = 0;
int specialCharCount = 0;
System.out.println("Please enter in a Password: ");
String test = in.next();
len = test.length();
for (int x = 0; x < len; x++) {
if (Character.isUpperCase(test.charAt(x))){ // tests characters at x position in string
upperCount++;
}else if (Character.isLowerCase(test.charAt(x))){ // tests characters at x position in string
lowerCount++;
}else if (Character.isDigit(test.charAt(x))) { // tests characters at x position in string
digCount++;
}else {
specialCharCount++;
}
}
if (len < 8) {
System.out.println("length of password should be at least 8");
continue;
}else if(upperCount < 2) {
System.out.println("Password should have at least 2 upper case");
continue;
}else if(lowerCount < 2) {
System.out.println("Password should have at least 2 lower case.");
continue;
}else if(digCount < 2) {
System.out.println("Password should have at least 2 digits");
continue;
}else if(specialCharCount < 2) {
System.out.println("Password should have at least 2 special charachter");
continue;
}else {
System.out.println("Valid Password.");
isValid = true;
}
} while (!isValid);
}
}
Sample Run :
This program will allow you to enter a new password
Your password must include 2 uppercase, 2 lowercase, 2 numbers, and 2 special characters.
Please enter in a Password:
yyyyyyyy
Password should have at least 2 upper case
Please enter in a Password:
yyYYYYYY
Password should have at least 2 digits
Please enter in a Password:
yyY88Ykk
Password should have at least 2 special charachter
Please enter in a Password:
jYj8Y8)k
Password should have at least 2 special charachter
Please enter in a Password:
jYj8Y8)*
Valid Password.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.