Question

Write a Java program that prompts the user to enter a password that matches a specific...

Write a Java program that prompts the user to enter a password that matches a specific pattern. Your program must approve the user's entry.. Here is the pattern, in this order:

  • 1 or more upper case letters
  • two lower case letters
  • 1 or 2 digits
  • zero or 1 upper case letters
  • any two of this group @#$%^&
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//PatternMatching.java


import java.util.Scanner;
import java.util.regex.*;//Java Regex or Regular Expression is an API to define a pattern for searching or manipulating strings.
public class Recursion1 {

   public static void main(String[] args) {
      
       Scanner s = new Scanner(System.in); //Scanner object to input password
       System.out.println("Enter passwrod : ");
       String password = s.next();
       Pattern pattern = Pattern.compile("[A-Z]+");   //compiling the Pattern with the regex(the pattern to be searched within password
       Matcher matcher = pattern.matcher(password);   //matcher object to find for the given pattern(/regex)
       if(matcher.find())               //returns true if the given pattern exists within the string (password)
       {
           System.out.println("Password Approved");
       }
      
       pattern = Pattern.compile("[a-z]{2}");   //compiling the Pattern with the regex(the pattern to be searched within password
       matcher = pattern.matcher(password);   //matcher object to find for the given pattern(/regex)
       if(matcher.find())               //returns true if the given pattern exists within the string (password)
       {
           System.out.println("Password Approved");
       }
       pattern = Pattern.compile("[0-9]{1,2}");   //compiling the Pattern with the regex(the pattern to be searched within password
       matcher = pattern.matcher(password);   //matcher object to find for the given pattern(/regex)
       if(matcher.find())               //returns true if the given pattern exists within the string (password)
       {
           System.out.println("Password Approved");
       }
       pattern = Pattern.compile("[0-9]{1,2}");   //compiling the Pattern with the regex(the pattern to be searched within password
       matcher = pattern.matcher(password);   //matcher object to find for the given pattern(/regex)
       if(matcher.find())               //returns true if the given pattern exists within the string (password)
       {
           System.out.println("Password Approved");
       }
       pattern = Pattern.compile("[A-Z]?");   //compiling the Pattern with the regex(the pattern to be searched within password
       matcher = pattern.matcher(password);   //matcher object to find for the given pattern(/regex)
       if(matcher.find())               //returns true if the given pattern exists within the string (password)
       {
           System.out.println("Password Approved");
       }
       pattern = Pattern.compile("[@#$%^&]{2}");   //compiling the Pattern with the regex(the pattern to be searched within password
       matcher = pattern.matcher(password);   //matcher object to find for the given pattern(/regex)
       if(matcher.find())               //returns true if the given pattern exists within the string (password)
       {
           System.out.println("Password Approved");
       }
       pattern = Pattern.compile("[A-Z]+[a-z]{2}[\\\\d]{1,2}[A-Z]{0,1}[@#$%^&]{2}");   //compiling the Pattern with the regex(the pattern to be searched within password
       matcher = pattern.matcher(password);   //matcher object to find for the given pattern(/regex)
       if(matcher.find())               //returns true if the given pattern exists within the string (password)
       {
           System.out.println("Password Approved");
       }
      
//       System.out.println(Pattern.matches("[A-Z]+",password)); //1 or more uppercase letters
//       System.out.println(Pattern.matches("[a-z]{2}", password)); //two lower case letters
//       System.out.println(Pattern.matches("[\\d]{1,2}",password));//1 or 2 digits
//       System.out.println(Pattern.matches("[A-Z]?",password));//zero or 1 upper case letters
//       System.out.println(Pattern.matches("[@#$%^&]{2}", password));//any two of this group @#$%^&
//       System.out.println(Pattern.matches("[A-Z]+[a-z]{2}[\\d]{1,2}[A-Z]{0,1}[@#$%^&]{2}",password));
      
       if(password.matches("[A-Z]+[a-z]{2}[\\d]{1,2}[A-Z]{0,1}[@#$%^&]{2}"))
       {
           //if the password matches all of the above patterns in the same order as given
           System.out.println("Password Approved");
       }
       /*
       * It is not clearly understood if the question asks to approve the password only when all of the above pattern matches in the
       * exact same order as given. If so the answer is in the above if{} condition. Rest individual conditions check have been
       * applied has well so as to understand how pattern matching is working. You can combine the results to achieve desired results.
       */
   }
}

Add a comment
Know the answer?
Add Answer to:
Write a Java program that prompts the user to enter a password that matches a specific...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT