Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class FancyBarcodes {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = Integer.parseInt(sc.nextLine());
- for (int i = 0; i < n; i++) {
- String barcode = sc.nextLine();
- Pattern pattern = Pattern.compile("@(#+)(?<barcode>[A-Z][A-Za-z0-9]{4,}[A-Z])\\b@(\\1)");
- Matcher matcher = pattern.matcher(barcode);
- StringBuilder productGroup = new StringBuilder();
- int secondCounter = 0;
- int counter = 0;
- String newBarcode = "";
- if (!matcher.matches()) {
- System.out.println("Invalid barcode");
- }
- if (matcher.matches()) {
- newBarcode = matcher.group("barcode");
- for (int j = 0; j < newBarcode.length(); j++) {
- char a = newBarcode.charAt(j);
- if (Character.isDigit(a)) {
- productGroup.append(a);
- secondCounter++;
- } else {
- counter++;
- }
- }
- if (secondCounter > 0) {
- counter = 0;
- }
- if (counter > 0) {
- System.out.println("Product Group: 00");
- } else {
- System.out.printf("Product Group: %s%n", productGroup);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement