CR7CR7

fancyBarcode

Nov 30th, 2022
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class test {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int n = Integer.parseInt(scanner.nextLine());
  10.    
  11.  
  12.  
  13.         for (int i = 0; i < n; i++) {
  14.             String input = scanner.nextLine();
  15.             String regex = "(?<symbol>[@])[#]+(?<words>[A-Z][A-Za-z0-9A-Z]{4,}[A-Z])(\\1)[#]+";
  16.             Pattern pattern = Pattern.compile(regex);
  17.             Matcher matcher = pattern.matcher(input);
  18.  
  19.  
  20.            if (matcher.find()) {
  21.                 boolean isInvalid = false;
  22.                 String barcodeProduct = "";
  23.                 String code = matcher.group("words");
  24.                 for (int j = 0; j < code.length(); j++) {
  25.  
  26.                     char barcodes = code.charAt(j);
  27.                     if (Character.isDigit(barcodes)) {
  28.                         barcodeProduct += barcodes;
  29.                     }
  30.                 }
  31.                 if (!barcodeProduct.equals("")) {
  32.                     System.out.printf("Product group: %s%n", barcodeProduct);
  33.                 } else {
  34.                     System.out.println("Product group: 00");
  35.                 }
  36.  
  37.             }else {
  38.                 System.out.println("Invalid barcode");
  39.  
  40.             }
  41.  
  42.         }
  43.     }
  44. }
Add Comment
Please, Sign In to add comment