Advertisement
IrinaIgnatova

FinalExam - Message Decripter

Aug 3rd, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.util.*;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10.  
  11.     public static void main(String[] args) {
  12.  
  13.         Scanner scanner = new Scanner(System.in);
  14.  
  15.         int n = Integer.parseInt(scanner.nextLine());
  16.         String regex = "^(\\$|%)([A-Z][a-z]{3,})\\1:\\s(\\[(\\d+)\\]\\|)(\\[(\\d+)\\]\\|)(\\[(\\d+)\\]\\|)$";
  17.         Pattern pattern = Pattern.compile(regex);
  18.         int firstNumberInText = 0;
  19.         int secondNumberInText = 0;
  20.         int thirdNumberInText = 0;
  21.  
  22.  
  23.         for (int i = 0; i < n; i++) {
  24.             String input = scanner.nextLine();
  25.  
  26.             Matcher matcher = pattern.matcher(input);
  27.             if (matcher.find()) {
  28.  
  29.                 String tagName = matcher.group(2);
  30.                 firstNumberInText = Integer.parseInt(matcher.group(4));
  31.                 secondNumberInText = Integer.parseInt(matcher.group(6));
  32.                 thirdNumberInText = Integer.parseInt(matcher.group(8));
  33.                 char firstAscii = (char) (firstNumberInText);
  34.                 char secondAscii = (char) (secondNumberInText);
  35.                 char thirdAscii = (char) (thirdNumberInText);
  36.                 System.out.printf("%s: %c%c%c%n", tagName, firstAscii, secondAscii, thirdAscii);
  37.  
  38.  
  39.             } else {
  40.                 System.out.println("Valid message not found!");
  41.             }
  42.  
  43.  
  44.         }
  45.  
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement