Need help with Java programming; Password Verification Program
1. Write a program that promts the user to enter a password. 2. Creas a boolean variable named valid anc set it to true. If any of these tests below fail, set it to true. 3. Check the password to see if it has at least 8 characters. If it deos not, display the message, "Password must have at least 8 characters" 4. Check the password to see f it consists of only letter and digits. To do this, you woll need to loop through all of the characters in the string. A character c is a letter of digit if this expression is true: ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') if this is even not true, break from your loop and display the message, "Password must contain only letter and digits" 5. If valid is still true at the end of the program, display the message, "Password accepted!"
The code I have so far: import java.util.Scanner; public class PasswordVerification { ; public static void main(String[] args) { // Creates a scanner Scanner sc = new Scanner(System.in); boolean valid = true; String password; // Asks user to enter password System.out.print("Please enter password and then hit enter:"); password = sc.nextLine(); // Checks to see if password is at least 8 characters. if (password.length()>=8) { valid = false; } // Checks each character to see if it is acceptable. for (int i = 0; i < password.length() ; i++) { char c = password.charAt(i); if (('a' <= c && c <= 'z') // Checks if it is a lower case letter || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter || ('0' <= c && c <= '9')) //Checks to see if it is a digit { valid = true; } else { // tell the user that only letters & digits are allowed System.out.println("Only letter & digits are acceptable."); valid = false; break; } } // if the password is valid, tell the user it's accepted } } I need help with 3 and 4! @SapphireMoon
Your code is pretty much there. The logic you need should become apparent if the indentation of the code blocks are cleaned up, and some more commenting is done.
Could you help me with it? @rsmith6559 When I put: // Checks to see if password is at least 8 characters. if (password.length()>=8) { valid = false; System.out.println("Password must have at least 8 characters"); } it prints the phrase every time I run it. If I type "Hello" for the user input, the message does not display. But when I put"Hellooooo" it displays the message. And that should not be the case. I tried doing an else statement and put that phrase in it but that seems to not work either. So could you help me with figuring this out? Thanks in advanced!
pro tip \``` Code here \```
Read the if statement VERY carefully. Computers don't do what you want, they do what you tell them to do. The problem with the if statement is a common error that you'll make many times, I certainly do. Making you find it may seem cruel, but learning how to read your code without "fixing" it will serve you well forever.
I have already figured it out with some help from people in StackOverflow and my program works like it should. =) Thank you for all of your help though! I really appreciate it! This is going to be my last Java question since this was my final project in this class and I am all done with it. Next semester is Web Design, but I already know HTML and CSS. But might need some help with that, only time will tell! Again, thank you!
Join our real-time social learning platform and learn together with your friends!