veronikaaa86

Problem 3 Regex

Mar 5th, 2018
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. package A_TheExam;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.List;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11.  
  12. public class P03 {
  13.     public static void main(String[] args) throws IOException {
  14.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  15.  
  16.         int n = Integer.parseInt(reader.readLine());
  17.  
  18.         List<String> attackerList = new ArrayList<>();
  19.         List<String> destroyList = new ArrayList<>();
  20.         String message = "";
  21.         int newLetter = 0;
  22.         String newStr = "";
  23.         for (int i = 0; i < n; i++) {
  24.             message = reader.readLine();
  25.  
  26.             int countLetters = 0;
  27.  
  28.             for (int j = 0; j < message.length(); j++) {
  29.                 //char test = message.charAt(j);
  30.                 if (message.charAt(j) == 's' ||
  31.                         message.charAt(j) == 't' ||
  32.                         message.charAt(j) == 'a' ||
  33.                         message.charAt(j) == 'r' ||
  34.                         message.charAt(j) == 'S' ||
  35.                         message.charAt(j) == 'T' ||
  36.                         message.charAt(j) == 'A' ||
  37.                         message.charAt(j) == 'R') {
  38.  
  39.                     countLetters++;
  40.                 }
  41.             }
  42.  
  43.             StringBuilder sb = new StringBuilder();
  44.             for (int j = 0; j < message.length(); j++) {
  45.                 newLetter = message.charAt(j) - countLetters;
  46.                 StringBuilder newStrBuild = sb.append((char) newLetter);
  47.                 newStr = newStrBuild.toString();
  48.             }
  49.  
  50.             String regex = "[^@\\-\\:\\!,\\>]*@([A-Za-z]+)" +
  51.                     "[^@\\-\\:\\!,\\>]*\\:([0-9]+)" +
  52.                     "[^@\\-\\:\\!,\\>]*\\!([AD])\\!" +
  53.                     "[^@\\-\\:\\!,\\>]*(->)([0-9]+)";
  54.  
  55.             Pattern pattern = Pattern.compile(regex);
  56.             Matcher matcher = pattern.matcher(newStr);
  57.  
  58.  
  59.             while (matcher.find()) {
  60.                 String type = matcher.group(3);
  61.                 String name = matcher.group(1);
  62.  
  63.                 if (type.equals("A")){
  64.                     attackerList.add(name);
  65.                     Collections.sort(attackerList);
  66.                 } else if (type.equals("D")) {
  67.                     destroyList.add(name);
  68.                     Collections.sort(destroyList);
  69.                 }
  70.             }
  71.         }
  72.  
  73.         System.out.printf("Attacked planets: %d%n", attackerList.size());
  74.  
  75.         attackerList
  76.                 .stream()
  77.                 .sorted((s1,s2)->s1.compareTo(s2))
  78.                 .forEach(s->System.out.printf("-> %s%n", s));
  79.  
  80.         System.out.printf("Destroyed planets: %d%n", destroyList.size());
  81.  
  82.         destroyList
  83.                 .stream()
  84.                 .sorted((s1,s2)->s1.compareTo(s2))
  85.                 .forEach(s->System.out.printf("-> %s%n", s));
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment