RGAlex

O2. Fancy Barcodes / FinalExam - O4

Mar 25th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. package FinalExam.O4;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class O2FancyBarcodes {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         int n = Integer.parseInt(scanner.nextLine());
  11.         for (int i = 0; i < n; i++) {
  12.             String input = scanner.nextLine();
  13.             Matcher matcher = Pattern.compile("(@#+)([A-Z][A-Za-z0-9]{4,}[A-Z])(@#+)").matcher(input);
  14.             if (matcher.find()) {
  15.                 Pattern patternDigit = Pattern.compile("\\d");
  16.                 matcher = patternDigit.matcher(input);
  17.                 String result = "";
  18.                 while (matcher.find()) {
  19.                     result += matcher.group();
  20.                 }
  21.                 if (result.equals("")) {
  22.                     result = "00";
  23.                 }
  24.                 System.out.println("Product group: " + result);
  25.  
  26.             } else {
  27.                 System.out.println("Invalid barcode");
  28.             }
  29.         }
  30.     }
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment