Password Verification Program Java -
i working on final project java class , need little help! first going show instructions, code , problem.
instructions
- write program prompts user enter password.
- create boolean variable named valid , set true. if of these tests below fail, set true.
- check password see if has @ least 8 characters. if not, display message, "password must have @ least 8 characters"
check password see if consists of letter , digits. this, need loop through of characters in string. character c letter of digit if expression true:
('a' <= c && c <= 'z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9')
if not true, break loop , display message, "password must contain letter , digits" 5. if valid still true @ end of program, display message, "password accepted!"
my code
import java.util.scanner; public class passwordverification { public static void main(string[] args) { // creates scanner scanner sc = new scanner(system.in); boolean valid = false; string password; // asks user enter password system.out.print("please enter password , hit enter:"); password = sc.nextline(); // checks see if password @ least 8 characters. if (password.length()<8) { valid = false; system.out.println("password must have @ least 8 characters"); } // checks each character see if acceptable. (int = 0; < password.length(); i++){ char c = password.charat(i); if ( ('a' <= c && c <= 'z') // checks if lower case letter || ('a' <= c && c <= 'z') //checks if upper case letter || ('0' <= c && c <= '9') //checks see if digit ) { valid = true; } else { // tells user letters & digits allowed system.out.println("only letter & digits acceptable."); valid = false; break; } } // if password valid, tell user it's accepted system.out.println("password accepted"); } }
so there instructions , code have far. sooooo close being done having problems part four. expected output should be:
please enter password: abc password have @ least 8 characters please enter password: abcd1234$ password must contain letter , digits please enter password: #### password must have @ least 8 characters password must contain letters , digits please enter password: abcd1234 password accepted!
when type abc get:
please enter password , hit enter:abc password must have @ least 8 characters password accepted
when program ends! me this, please? in advanced. =) also, java programming class.
as @cralfaro stated have repeat process if password invalid:
import java.util.scanner; public class passwordverification { public static void main(string[] args) { // creates scanner scanner sc = new scanner(system.in); boolean valid = false; string password; { // start loop // asks user enter password system.out.print("please enter password , hit enter:"); password = sc.nextline(); // checks see if password @ least 8 characters. if (password.length()<8) { valid = false; system.out.println("password must have @ least 8 characters"); continue; // skip next iteration } // checks each character see if acceptable. (int = 0; < password.length(); i++){ char c = password.charat(i); if ( ('a' <= c && c <= 'z') // checks if lower case letter || ('a' <= c && c <= 'z') //checks if upper case letter || ('0' <= c && c <= '9') //checks see if digit ) { valid = true; } else { // tells user letters & digits allowed system.out.println("only letter & digits acceptable."); valid = false; break; } } } while(!valid); // verify if password valid, if not repeat process // if password valid, tell user it's accepted system.out.println("password accepted"); } }
in way program continue ask user input if password not valid.
edit: gc_'s comment, problem missed continue statement in first check.
Comments
Post a Comment