Advertisement
BojidarDosev

barcode validation

Mar 26th, 2025
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. import java.sql.Array;
  2. import java.util.*;
  3.  
  4. public class test {
  5.     public static void main(String[] args) {
  6.         Scanner scan= new Scanner(System.in);
  7.  
  8.         int inputsCount = Integer.parseInt(scan.nextLine());
  9.         while (inputsCount > 0) {
  10.             StringBuilder input = new StringBuilder(scan.nextLine());
  11.             StringBuilder group = new StringBuilder();
  12.             boolean valid = true;
  13.  
  14.             //validation #1
  15.             for (int i = 0; i < input.length(); i++) {
  16.                 if (i == 0 && input.charAt(i) != '@') {
  17.                     valid = false;
  18.                 } else if (i == 1 && input.charAt(i) != '#') {
  19.                     valid = false;
  20.                 } else if (i == input.length() - 1 && input.charAt(i) != '#') {
  21.                     valid = false;
  22.                 }
  23.             }
  24.  
  25.             int br = 1;
  26.             int index1 = 0;
  27.             int index2 = 0;
  28.             //remove the first series of "@#..."
  29.             if (valid) {
  30.                 while (input.charAt(br) == '#') {
  31.  
  32.                     index1++;
  33.                     br++;
  34.                 }
  35.             }
  36.  
  37.             input.replace(0, index1 + 1, "").reverse();
  38.             //remove the last series of "@#..."
  39.             if (valid) {
  40.                 br = 0;
  41.                 while (input.charAt(br) == '#') {
  42.  
  43.                     index2++;
  44.                     br++;
  45.                 }
  46.             }
  47.             //validation #2
  48.             if (input.charAt(br) != '@') {
  49.                 valid = false;
  50.             }
  51.             input.replace(0, index2 + 1, "").reverse();
  52.             //validation #3
  53.             if (input.length() < 6) {
  54.                 valid = false;
  55.             }
  56.             //validation #4
  57.             char firstLetter = input.charAt(0);
  58.             if (!Character.isUpperCase(firstLetter)) {
  59.                 valid = false;
  60.             }
  61.             //validation #5
  62.             for (int i = 0; i < input.length(); i++) {
  63.                 if (!Character.isLetterOrDigit(input.charAt(i))) {
  64.                     valid = false;
  65.                 }
  66.             }
  67.             //validation #6
  68.             char lastLetter = input.charAt(input.length() - 1);
  69.             if (!Character.isUpperCase(lastLetter)) {
  70.                 valid = false;
  71.             }
  72.  
  73.             for (int i = 0; i < input.length(); i++) {
  74.                 if (Character.isDigit(input.charAt(i))) {
  75.                     group.append(input.charAt(i));
  76.                 }
  77.             }
  78.             if(group.isEmpty()){
  79.                 group.append("00");
  80.             }
  81.             if (valid) {
  82.                 System.out.println("Product group: " + group);
  83.             }
  84.             else{
  85.                 System.out.println("Invalid barcode");
  86.             }
  87.             inputsCount--;
  88.         }
  89.  
  90.     }
  91. }
  92. /*
  93. 3
  94. @#FreshFisH@#
  95. @###Brea0D@###
  96. @##Che4s6E@##
  97.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement