I need a method that can tell me if a String has non alphanumeric characters. I used the ascii table to look up to check if the string has these: !@#$%^&*() <---- I NEED THE PROGRAM TO CHECK IF IT HAS AT LEAST ONE OF THESE.
THANKS! This is what i have so far but it doesnt seem to work:
import java.util.Scanner; // Imports the Scanner class to get Keyboard Inputs class ValidatePassword { public static void main (String [] args) { String inputPassword; // Creates the Password variable Scanner input = new Scanner (System.in); // Creates a new Scanner System.out.println("The password must be: 1) Between 8 and 20 characters "); System.out.print("2) It must contain at least 1 uppercase and 1 lowercase character "); System.out.print("3)It must contain at least 1 digit "); System.out.print("4)It must contain at least one non-alphanumeric but printable character (like those characters over the digits on your keyboard)"); System.out.println("Password: "); // Prints the word "Password" to the screen inputPassword = input.next(); // Gets the user input for the password System.out.println(PassCheck(inputPassword)); // Calls the PassCheck Method on the password entered by the user and prints result to screen System.out.println(""); main(args); // re-runs the program (Allows for multiple tests) } public static String PassCheck (String Password) { String result = "Valid Password"; // Sets the initial result as valid int length = 0; // Stores the number characters in the password int numCount = 0; // Variable used to store numbers in the password int capCount = 0; // Variable used to store capital letters in the password int lowCount=0; int Alpha=0; for (int x =0; x < Password.length(); x++) { if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58) || (Password.charAt(x) >= 64 && Password.charAt(x) <= 91) || (Password.charAt(x) >= 96 && Password.charAt(x) <= 123) || (Password.charAt(x) >= 97 && Password.charAt(x) <= 122) || (Password.charAt(x) >= 32 && Password.charAt(x) <= 48)) { //Keep the Password } else { result = "Password Contains Invalid Character!"; //Checks that password contains only letters and numbers } if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) { // Counts the number of numbers numCount ++; } if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) { // Counts the number of capital letters capCount ++; } if ((Password.charAt(x) > 96 && Password.charAt(x) < 123)) { // Counts the number of lowercase letters lowCount ++; } if ((Password.charAt(x) > 32 && Password.charAt(x) < 48 )) { // Counts the number of lowercase letters Alpha ++; } length = (x + 1); // Counts the passwords length } // Ends the for loop if (numCount < 1){ // Checks that password contains 1 numbers result = "You need at least 1 number in your password!"; } if (length < 8){ //checks if the password is too short result = "Too short! Password has to be at least 8 characters long"; } if (length > 20){ //checks if the password is too long result = "Too long!"; } if(Alpha < 0){ result = " You need at least one non-alphanumeric!"; } if (lowCount < 1){ //checks if their is at least 1 lowercase letter result = "You need at least 1 lowercase letter in your password!"; } if (capCount < 1) { // Checks that password contains at least 1 capital letter result = "You need at least 1 capital letter in your password!"; } if (length < 8){ // Checks that password is long enough result = "Password is too Short!"; } return (result); // Returns the value of "result" } // Ends the PassCheck method } // Ends the ValidatePassword class
When I try it out I type in for example: ddddddddddD3
Valid Password
Doesnt even seem to be looking for it. Please just help me out.