Advertisement
Guest User

FancyBarcodes

a guest
Apr 4th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 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 scanner = new Scanner(System.in);
  8.         String regex = "^(@#+)(?<barcode>[A-Z][A-Za-z\\d]{4,}[A-Z])(@#+)$";
  9.         Pattern pattern = Pattern.compile(regex);
  10.         Pattern digits = Pattern.compile("(?<productGroup>[\\d]+)");
  11.  
  12.         int n = Integer.parseInt(scanner.nextLine());
  13.  
  14.         for (int i = 0; i < n; i++) {
  15.             String input = scanner.nextLine();
  16.             Matcher matcher = pattern.matcher(input);
  17.  
  18.             if (matcher.find()) {
  19.                 String barcode = matcher.group("barcode");
  20.                 Matcher digitMatcher = digits.matcher(barcode);
  21.  
  22.                 if (digitMatcher.find()) {
  23.                     String productGroup = barcode.replaceAll("[@#A-Za-z]+", "");
  24.                     System.out.println(String.format("Product group: %s", productGroup));
  25.                 } else {
  26.                     System.out.println("Product group: 00");
  27.                 }
  28.  
  29.             } else {
  30.                 System.out.println("Invalid barcode");
  31.             }
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement