desislava_topuzakova

02. Emoji Detector

Nov 26th, 2021
1,657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. package ExamPreparation;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class EmojiDetector_02 {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         String text = scanner.nextLine();
  13.  
  14.         String regexEmoji = "([:]{2}|[*]{2})(?<emoji>[A-Z][a-z]{2,})(\\1)";
  15.         Pattern pattern = Pattern.compile(regexEmoji);
  16.         Matcher matcher = pattern.matcher(text);
  17.  
  18.         int coolThreshold = 1; //праг на готиност
  19.         for (char symbol : text.toCharArray()) {
  20.             if (Character.isDigit(symbol)) {
  21.                 //'1' -> "1" -> 1
  22.                 coolThreshold *= Integer.parseInt(symbol + "");
  23.             }
  24.         }
  25.         System.out.printf("Cool threshold: %d%n", coolThreshold);
  26.  
  27.         int countValidEmoji = 0;
  28.         List<String> coolEmoji = new ArrayList<>(); //готини емоджита -> сума за емоджито >= coolThreshold
  29.  
  30.         while(matcher.find()) { //true -> валидно емоджи
  31.             countValidEmoji++;
  32.             //сума на емоджито
  33.             String emoji = matcher.group("emoji");
  34.             int emojiSum = 0;
  35.             for (char symbol : emoji.toCharArray()) {
  36.                 emojiSum += (int) symbol;
  37.             }
  38.             //проверка дали емоджито е cool
  39.             if (emojiSum >= coolThreshold) {
  40.                 coolEmoji.add(matcher.group());
  41.             }
  42.         }
  43.  
  44.         System.out.printf("%d emojis found in the text. The cool ones are:%n", countValidEmoji);
  45.         coolEmoji.forEach(emoji -> System.out.println(emoji));
  46.  
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment