Advertisement
yocalee

Untitled

Apr 1st, 2020
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. package exam;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import java.util.Scanner;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11. public class HeartDelivery {
  12.     public static void main(String[] args) {
  13.         Scanner sc = new Scanner(System.in);
  14.  
  15.         int n = Integer.parseInt(sc.nextLine());
  16.         Pattern pattern = Pattern.compile("^(U\\$)(?<username>[A-Z][a-z]{3,})\\1(P@\\$)(?<password>[A-Za-z]{5,}[0-9]+)\\3$");
  17.         int sr = 0;
  18.         for (int i = 0; i < n; i++) {
  19.             Matcher matcher = pattern.matcher(sc.nextLine());
  20.             if (isValid(matcher)==true){
  21.  
  22.                     String username = matcher.group("username");
  23.                     String password = matcher.group("password");
  24.                     System.out.println("Registration was successful");
  25.                     System.out.println("Username: "+username +", Password: "+password);
  26.                     sr++;
  27.             }else {
  28.                 System.out.println("Invalid username or password");
  29.             }
  30.         }
  31.             System.out.println("Successful registrations: " + sr);
  32.  
  33.  
  34.     }
  35.  
  36.     private static boolean isValid(Matcher matcher) {
  37.       if (matcher.matches()){
  38.           return true;
  39.       }else {
  40.           return false;
  41.       }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement