Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package JavaFundamentals.FinalExamExamples;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class FancyBarcodes_02 {
- public static void main(String[] args) {
- Scanner read = new Scanner(System.in);
- // regex --> "@#+(?<barcode>[a-zA-Z0-9A-Z]{6,})@#+"
- int loop = Integer.parseInt(read.nextLine());
- for (int index = 0; index < loop; index++) {
- // inside the loop we read barcode and compile the regex
- String barcode = read.nextLine();
- Matcher matcher = Pattern.compile("@#+(?<barcode>[a-zA-Z0-9A-Z]{6,})@#+").matcher(barcode);
- if (matcher.find()){
- matcher = Pattern.compile("\\d").matcher(barcode); // --> taking the digit from the regex
- StringBuilder group = new StringBuilder();
- while(matcher.find()){
- group.append(matcher.group());
- }
- if (group.toString().length() == 0){
- group = new StringBuilder("00");
- }
- System.out.printf("Product group: %s%n",group.toString());
- }else{
- System.out.println("Invalid barcode");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement