desislava_topuzakova

02. Fancy Barcodes

Mar 26th, 2021
1,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. package Zoo_02;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class FancyBarcodes_02 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         int n = Integer.parseInt(scanner.nextLine());
  11.  
  12.         String productRegex = "@#+(?<product>[A-Z][a-zA-Z0-9]{4,}[A-Z])@#+";
  13.         Pattern pattern = Pattern.compile(productRegex);
  14.  
  15.         for (int i = 0; i < n; i++) {
  16.             String barcode = scanner.nextLine();
  17.             Matcher matcher = pattern.matcher(barcode);
  18.             if(matcher.find()){
  19.                 //валиден
  20.                 String product = matcher.group("product");
  21.                 String productGroup = "";
  22.                 //"FreshFisH" -> ""
  23.                 //"Brea0D" -> "0"
  24.                 //"Che4s6E" -> "46"
  25.                 for (int index = 0; index <= product.length() - 1; index++) {
  26.                     char currentSymbol = product.charAt(index);
  27.                     if(Character.isDigit(currentSymbol)) {
  28.                         productGroup += currentSymbol;
  29.                     }
  30.  
  31.                 }
  32.                 //ако нямаме цифри
  33.                 if(productGroup.equals("")) {
  34.                     System.out.println("Product group: 00");
  35.                 } else {
  36.                     System.out.println("Product group: " + productGroup);
  37.                 }
  38.  
  39.             } else {
  40.                 //невалиден
  41.                 System.out.println("Invalid barcode");
  42.             }
  43.  
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment