Advertisement
mark79

Java Fundamentals - The Isle of Man TT Race

Aug 1st, 2019
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 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 TheIsleOfManTTRace_v2 {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         while (true) {
  10.             String input = sc.nextLine();
  11. //            String regex = "(^[#$%*&])(?<name>[A-Za-z]+)\\1=(?<length>\\d+)!!(?<geohashcode>.+)$";
  12.             String regex = "([#$%*&]){1}(?<name>[A-Za-z]+)\\1=(?<length>[0-9]+)!!(?<geohashcode>.*)$";
  13.             Pattern pattern = Pattern.compile(regex);
  14.             Matcher matcher = pattern.matcher(input);
  15.             if (matcher.find() && Integer.parseInt(matcher.group("length")) == matcher.group("geohashcode").length()) {
  16.                 String name = matcher.group("name");
  17.                 int length = Integer.parseInt(matcher.group("length"));
  18.                 String geoHashCodeEncrypted = matcher.group("geohashcode");
  19.                 StringBuilder sb = new StringBuilder();
  20.                 for (int i = 0; i < length; i++) {
  21.                     char ch = geoHashCodeEncrypted.charAt(i);
  22.                     sb.append((char) (ch + length));
  23.                 }
  24.                 System.out.println("Coordinates found! " + name + " -> " + sb.toString());
  25.                 return;
  26.             } else {
  27.                 System.out.println("Nothing found!");
  28.             }
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement