StefanShivarov

Message Encrypter

Nov 28th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class message_encrypter {
  6.  
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         int n = Integer.parseInt(scanner.nextLine());
  13.  
  14.         String regex = "([*@])(?<tag>[A-Z][a-z]{2}[a-z]+)\\1: \\[(?<letter1>[A-z])\\]\\|\\[(?<letter2>[A-z])\\]\\|\\[(?<letter3>[A-z])\\]\\|$";
  15.         Pattern pattern = Pattern.compile(regex);
  16.  
  17.  
  18.         for(int i = 0; i < n; i++){
  19.  
  20.             String input = scanner.nextLine();
  21.  
  22.             Matcher matcher = pattern.matcher(input);
  23.  
  24.             if(matcher.find()) {
  25.  
  26.                 String tag = matcher.group("tag");
  27.                 int letter1Value = (int)matcher.group("letter1").charAt(0);
  28.                 int letter2Value = (int)matcher.group("letter2").charAt(0);
  29.                 int letter3Value = (int)matcher.group("letter3").charAt(0);
  30.  
  31.  
  32.                 System.out.println(tag + ": " + letter1Value + " " + letter2Value + " " + letter3Value);
  33.  
  34.             }else{
  35.  
  36.                 System.out.println("Valid message not found!");
  37.             }
  38.  
  39.  
  40.  
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment