Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions