Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Exam;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class FancyBarcodes {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String regex = "@[#]{1,}[A-Z]{1}[A-Za-z0-9]{4,}[A-Z]{1}@[#]{1,}";
- Pattern pattern = Pattern.compile(regex);
- int numberOfProducts = Integer.parseInt(scanner.nextLine());
- for (int i = 0; i < numberOfProducts; i++) {
- String current = scanner.nextLine();
- Matcher matcher = pattern.matcher(current);
- if (matcher.find()) {
- StringBuilder nums = new StringBuilder();
- current.chars().filter(Character::isDigit).forEach(e -> {
- nums.append(String.valueOf((char) e));
- });
- System.out.println("Product group: " + ((nums.toString().trim().isEmpty()) ? "00" : nums.toString()));
- } else {
- System.out.println("Invalid barcode");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment