Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Pr2ActivationKeys {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String line = scanner.nextLine();
- String[] arrLine = line.split("&");
- String regex = "^[A-Za-z0-9]+$";
- Pattern pattern = Pattern.compile(regex);
- StringBuilder resultSB = new StringBuilder();
- StringBuilder resultFinal = new StringBuilder();
- for (int i = 0; i < arrLine.length; i++) {
- String badKey = arrLine[i];
- Matcher matcher = pattern.matcher(badKey);
- if (matcher.find()) {
- String key = matcher.group().toUpperCase();
- int keyLenght = key.length();
- if (keyLenght == 16) {
- for (int j = 1; j <= keyLenght; j++) {
- resultSB.append(key.charAt(j - 1));
- if (j % 4 == 0) {
- if (!(j - 1 == keyLenght - 1)) {
- resultSB.append('-');
- }
- }
- }
- } else if (keyLenght == 25) {
- for (int j = 1; j <= keyLenght; j++) {
- resultSB.append(key.charAt(j - 1));
- if (j % 5 == 0) {
- if (!(j - 1 == keyLenght - 1)) {
- resultSB.append('-');
- }
- }
- }
- }
- key = resultSB.toString();
- resultSB.delete(0, resultSB.length());
- for (int j = 0; j < key.length(); j++) {
- if (Character.isDigit(key.charAt(j))) {
- int digit = key.charAt(j) - '0';
- digit = 9 - digit;
- resultSB.append(digit);
- } else {
- resultSB.append(key.charAt(j));
- }
- }
- //System.out.println(resultSB.toString());
- resultFinal.append(resultSB);
- resultFinal.append(", ");
- resultSB.delete(0, resultSB.length());
- }
- }
- String finalKeys = resultFinal.toString();
- finalKeys = finalKeys.substring(0, finalKeys.length()-2);
- System.out.println(finalKeys);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement