Advertisement
mirozspace

A_Keys

Apr 1st, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class Pr2ActivationKeys {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String line = scanner.nextLine();
  10.         String[] arrLine = line.split("&");
  11.         String regex = "^[A-Za-z0-9]+$";
  12.         Pattern pattern = Pattern.compile(regex);
  13.         StringBuilder resultSB = new StringBuilder();
  14.         StringBuilder resultFinal = new StringBuilder();
  15.  
  16.         for (int i = 0; i < arrLine.length; i++) {
  17.             String badKey = arrLine[i];
  18.             Matcher matcher = pattern.matcher(badKey);
  19.  
  20.             if (matcher.find()) {
  21.                 String key = matcher.group().toUpperCase();
  22.                 int keyLenght = key.length();
  23.                 if (keyLenght == 16) {
  24.                     for (int j = 1; j <= keyLenght; j++) {
  25.                         resultSB.append(key.charAt(j - 1));
  26.                         if (j % 4 == 0) {
  27.                             if (!(j - 1 == keyLenght - 1)) {
  28.                                 resultSB.append('-');
  29.                             }
  30.  
  31.                         }
  32.                     }
  33.                 } else if (keyLenght == 25) {
  34.                     for (int j = 1; j <= keyLenght; j++) {
  35.                         resultSB.append(key.charAt(j - 1));
  36.                         if (j % 5 == 0) {
  37.                             if (!(j - 1 == keyLenght - 1)) {
  38.                                 resultSB.append('-');
  39.                             }
  40.                         }
  41.                     }
  42.                 }
  43.                 key = resultSB.toString();
  44.                 resultSB.delete(0, resultSB.length());
  45.  
  46.                 for (int j = 0; j < key.length(); j++) {
  47.                     if (Character.isDigit(key.charAt(j))) {
  48.                         int digit = key.charAt(j) - '0';
  49.                         digit = 9 - digit;
  50.                         resultSB.append(digit);
  51.                     } else {
  52.                         resultSB.append(key.charAt(j));
  53.                     }
  54.                 }
  55.                 //System.out.println(resultSB.toString());
  56.  
  57.                 resultFinal.append(resultSB);
  58.                 resultFinal.append(", ");
  59.                 resultSB.delete(0, resultSB.length());
  60.             }
  61.         }
  62.         String finalKeys = resultFinal.toString();
  63.         finalKeys = finalKeys.substring(0, finalKeys.length()-2);
  64.         System.out.println(finalKeys);
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement