borovaneca

Fancy

Mar 31st, 2023
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. package Exam;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class FancyBarcodes {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.  
  12.  
  13.         String regex = "@[#]{1,}[A-Z]{1}[A-Za-z0-9]{4,}[A-Z]{1}@[#]{1,}";
  14.         Pattern pattern = Pattern.compile(regex);
  15.  
  16.         int numberOfProducts = Integer.parseInt(scanner.nextLine());
  17.  
  18.         for (int i = 0; i < numberOfProducts; i++) {
  19.             String current = scanner.nextLine();
  20.             Matcher matcher = pattern.matcher(current);
  21.             if (matcher.find()) {
  22.                 StringBuilder nums = new StringBuilder();
  23.                 current.chars().filter(Character::isDigit).forEach(e -> {
  24.                     nums.append(String.valueOf((char) e));
  25.                 });
  26.                 System.out.println("Product group: " + ((nums.toString().trim().isEmpty()) ? "00" : nums.toString()));
  27.             } else {
  28.                 System.out.println("Invalid barcode");
  29.             }
  30.         }
  31.     }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment