Some Websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rule is as follows:
- A password must have at least 8 characters
- A password consists of only letters and digits
- A password must contain at least 2 digits
Write a program that prompts the user to enter a password and displays "Valid Password" if the rule is followed or "Invalid Password" otherwise.
Here is my code:
import java.util.*; import java.lang.String; import java.lang.Character; public class CheckingPassword { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter a Password: "); String password = input.next(); if (isValid(password)) { System.out.println("Valid Password"); } else { System.out.println("Invalid Password"); } } public static boolean isValid(String password) { if (password.length() < 8) { return false; } char c; for (int i = 0; i < password.length() - 1; i++) { c = password.charAt(i); } if (c.isLetterOrDigit()) { return false; } else if (c.isDigit()) { return false; } else { return true; } } }
When I compile my code, I get the following error message:
How do I fix this error? Even if I fix this error, my code won't run correctly. I know there is something wrong with my isValid method. I can't quite figure it out. Also I don't know how to check to see if the password has at least 2 digits. How would I do that?CheckingPassword.java:29: char cannot be dereferenced
if (c.isLetterOrDigit()) {
^
CheckingPassword.java:31: char cannot be dereferenced
} else if (c.isDigit()) {
^
2 errors