Question
This is my lab assignment. Password Validation Write a program that verifies that passwords satisfy the following criteria: 1) At least eight characters long 2)
This is my lab assignment.
Password Validation Write a program that verifies that passwords satisfy the following criteria: 1) At least eight characters long 2) Contains at least one numeric digit 3) Contains at least one upper case and at least one lower case letter 4) Contains at least one of these special characters:
!@#$%&*:;
Your program should input a string and determine if it satisfies these criteria. If the password fails, the program should display a message indicating why.
This is my code. I don't understand why it's not working. Shouldn't it break the for loop once one of the characters fulfills it?
import java.util.Scanner;
class Main { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);
boolean valid = false; System.out.println("Create a Password"); String password = scnr.nextLine();
if (password.length() < 8) { System.out.println("You need at least 8 characters"); }
for (int i = 0; i < password.length(); i++) { char pw = password.charAt(i); if (('a' <= pw && pw <= 'z') || ('A' <= pw && pw <= 'Z')) { valid = true; break; } else { System.out.println("You need at least one lowercase and uppercase letter"); valid = false; break; } }
for (int i = 0; i < password.length(); i++) { char pw = password.charAt(i); if ('0' <= pw && pw <= '9') { valid = true; break; } else { System.out.println("You need at least one number"); valid = false; break; } }
for (int i = 0; i < password.length(); i++) { char pw = password.charAt(i); if ((pw == '!') || (pw == '@') || (pw == '#') || (pw == '$') || (pw == '%') || (pw == '&') || (pw == '*') || (pw == ';') || (pw == ':')) { valid = true; break; } else { System.out.println("You need at least one special charater"); valid = false; break; } }
if (valid == true) { System.out.println("Password Created"); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started