Advertisement
Guest User

FancyBarcodes

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