Didart

Emoji Detector - Other decision

Nov 26th, 2021 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class EmojiDetector2 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String text = scanner.nextLine();
  12.  
  13.         String regexEmoji = "([:]{2}|[*]{2})(?<emoji>[A-Z][a-z]{2,})(\\1)";
  14.  
  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.                 coolThreshold *= Integer.parseInt(symbol + "");
  22.             }
  23.         }
  24.  
  25.         System.out.printf("Cool threshold: %d%n", coolThreshold);
  26.  
  27.         int countValidEmoji = 0;
  28.         List<String> coolEmoji = new ArrayList<>();
  29.  
  30.         while (matcher.find()) {
  31.             countValidEmoji++;
  32.            
  33.             String emoji = matcher.group("emoji");
  34.            
  35.             int emojiSum = 0;
  36.             for (char symbol : emoji.toCharArray()) {
  37.                 emojiSum += (int)symbol;
  38.             }
  39.             if (emojiSum >= coolThreshold) {
  40.               coolEmoji.add(matcher.group());
  41.             }
  42.         }
  43.         System.out.printf("%d emojis found in the text. The cool ones are:%n", countValidEmoji);
  44.         coolEmoji.forEach(emoji -> System.out.println(emoji));
  45.     }
  46. }
  47.  
Add Comment
Please, Sign In to add comment