In: Computer Science
Imagine you are developing a software package for an online shopping site that requires users to enter their own passwords. Your software requires that users' passwords meet the following criteria: The password should be at least six characters long. The password should contain at least one uppercase and at least one lowercase letter. The password should have at least one digit. Write a class that verifies that a password meets the stated criteria. Demonstrate the class in a program that allows the user to enter a password and then displays a message indicating whether it is valid or not.
I wrote the following code and I am receiving errors, can someone debug this?
import java.util.Scanner;
class Main {
public static void main (String []args)
{
String password; // to hold input entered by the user
boolean hasupperletters = false;
boolean haslowerletters = false;
boolean hasdigits = false;
boolean wronginput = false;
//create a scanner object and ask for input.
Scanner keyboard = new Scanner(System.in);
System.out.print("Create your password");
password = keyboard.nextLine();
int passlength= password.length();
//loop to go through all characters in the password String
for (int i = 0; i < passLength; i++){
char c = input.charAt(i);
//check for at least one uppercase character
if(Character.isUpperCase(c))
hasupperletters = true;
//check for at least one lowercase character
else if (Character.isLowerCase(c))
haslowerletters=true;
//check for at least one digit
else if(Character.isDigit(c))
hasdigits = true;
}
//check for correctly formatted password
if(hasupperletters && hasdigits && haslowerletters && !wronginput && (passlength>=6))
{
System.out.println("Password is correctly formatted");
}
else
{
System.out.println("Password is not correctly formatted");
}
}
}
Explanation:
Here is the corrected code which takes the password, checks for uppercases, lowercases and digits and then prints whether the password is satisfactory or not.
Code:
import java.util.Scanner;
class Main {
public static void main (String []args)
{
String password; // to hold input entered by the user
boolean hasupperletters = false;
boolean haslowerletters = false;
boolean hasdigits = false;
boolean wronginput = false;
//create a scanner object and ask for input.
Scanner keyboard = new Scanner(System.in);
System.out.print("Create your password");
password = keyboard.nextLine();
int passlength= password.length();
//loop to go through all characters in the password String
for (int i = 0; i < passlength; i++){
char c = password.charAt(i);
//check for at least one uppercase character
if(Character.isUpperCase(c))
hasupperletters = true;
//check for at least one lowercase character
else if (Character.isLowerCase(c))
haslowerletters=true;
//check for at least one digit
else if(Character.isDigit(c))
hasdigits = true;
}
//check for correctly formatted password
if(hasupperletters && hasdigits && haslowerletters && !wronginput && (passlength>=6))
{
System.out.println("Password is correctly formatted");
}
else
{
System.out.println("Password is not correctly formatted");
}
}
}
output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!