Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class exam {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String code = "";
  7.         int n = Integer.parseInt(scanner.nextLine());
  8.  
  9.         for(int i = 0 ;i < n; i++) {
  10.             code ="";
  11.             String barcode = scanner.nextLine();
  12.  
  13.             char firstSymbol = barcode.charAt(0);
  14.  
  15.             if(firstSymbol != '@') {
  16.                 System.out.println("Invalid barcode");
  17.                 continue;
  18.             }
  19.  
  20.             char secondSymbol = barcode.charAt(1);
  21.  
  22.             if(secondSymbol != '#') {
  23.                 System.out.println("Invalid barcode");
  24.                 continue;
  25.             }
  26.  
  27.             String newWord = barcode.replace("@", "");
  28.             newWord = newWord.replace("#", "");
  29.             newWord = newWord.replace(" ", "");
  30.  
  31.             char firstLetter = newWord.charAt(0);
  32.  
  33.             if(Character.isLowerCase(firstLetter)) {
  34.                 System.out.println("Invalid barcode");
  35.                 continue;
  36.             }
  37.  
  38.             boolean isStupid = true;
  39.             for(int j= 0; j < newWord.length(); j++) {
  40.                 if(!Character.isLetterOrDigit((newWord.charAt(j)))) {
  41.                     System.out.println("Invalid barcode");
  42.                     isStupid = false;
  43.                 }
  44.             }
  45.  
  46.             if(isStupid == false) {
  47.                 continue;
  48.             }
  49.  
  50.             char lastLetter = newWord.charAt(newWord.length() - 1);
  51.  
  52.             if(Character.isLowerCase(lastLetter)) {
  53.                 System.out.println("Invalid barcode");
  54.                 continue;
  55.             }
  56.  
  57.             for(int k = 0; k < newWord.length(); k++) {
  58.  
  59.                 if(Character.isDigit(newWord.charAt(k))) {
  60.                     code += newWord.charAt(k);
  61.                 }
  62.             }
  63.  
  64.             if(code.isEmpty()) {
  65.                 code = "00";
  66.             }
  67.  
  68.             System.out.printf("Product group: %s%n", code);
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement