Advertisement
Didart

Fancy Barcodes

Nov 24th, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 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.  
  9.         int n = Integer.parseInt(scanner.nextLine());
  10.  
  11.         String regexValidBarcode = "@#+[A-Z][A-Za-z0-9]{4,}[A-Z]@#+";
  12.  
  13.         Pattern pattern = Pattern.compile(regexValidBarcode);
  14.         Matcher matcher = null;
  15.        
  16.         for (int i = 0; i < n; i++) {
  17.             String barcode = scanner.nextLine();
  18.  
  19.             matcher = pattern.matcher(barcode);
  20.             if (matcher.find()) {
  21.                 StringBuilder sb = new StringBuilder();
  22.                
  23.                 for (int j = 0; j < barcode.length(); j++) {
  24.                     char symbol = barcode.charAt(j);
  25.                     if (Character.isDigit(symbol)) {
  26.                         sb.append(symbol);
  27.                     }
  28.                 }
  29.                
  30.                 if (sb.length() == 0) {
  31.                     System.out.println("Product group: 00");
  32.                 } else {
  33.                     System.out.println("Product group: " + sb);
  34.                 }
  35.             } else {
  36.                 System.out.println("Invalid barcode");
  37.             }
  38.         }
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement