I am attempting to write a java program that acts as a \"Password\" tester. It n
ID: 3690320 • 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 GroupProject
{
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
import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
int flag=1;
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.");
while(flag==1)
{
flag=1;
System.out.println("Please enter in a Password: ");
String test = in.next();
int y = test.length();
//gets limit length for for loop
int count=0;
for (int x=0; x < y; x++)
{
if (Character.isUpperCase(test.charAt(x)))
//tests characters at x position in string
{
count++;
}
}
if(count ==0)
flag=1;
else
flag=0;
int count1=0;
for (int b=0; b < y; b++)
{
if (Character.isLowerCase(test.charAt(b)))
//tests characters at x position in string
{
count1++;
}
}
if(count1 ==0)
flag=1;
else
flag=0;
int count2=0;
for (int a=0; a<y; a++)
{
if (Character.isDigit(test.charAt(a)))
//tests characters at x position in string
{
count2++;
}
}
if(count2 ==0)
flag=1;
else
flag=0;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.