Advertisement
Guest User

second

a guest
Apr 4th, 2020
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 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 second {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int n = Integer.parseInt(scanner.nextLine());
  10.  
  11.         Pattern pattern = Pattern.compile("[@][#]+([A-Z][A-Za-z0-9]{4,}[A-Z])[@][#]+");
  12.  
  13.         while (n-- > 0) {
  14.  
  15.             String input = scanner.nextLine();
  16.  
  17.             Matcher matcher = pattern.matcher(input);
  18.  
  19.             if (matcher.find()) {
  20.  
  21.                 String product = matcher.group(1);
  22.                 StringBuilder code = new StringBuilder();
  23.  
  24.                 for (int i = 0; i < product.length(); i++) {
  25.                     char symbol = product.charAt(i);
  26.                     if (Character.isDigit(symbol)) {
  27.                         code.append(symbol);
  28.                     }
  29.                 }
  30.  
  31.                 if (code.length() > 0) {
  32.                     System.out.println(String.format("Product group: %s", code.toString()));
  33.                 } else if (code.length() == 0) {
  34.                     System.out.println("Product group: 00");
  35.                 }
  36.  
  37.             } else {
  38.                 System.out.println("Invalid barcode");
  39.             }
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement