LoraOrliGeo

P01_The Isle of Man TT Race_14April_group2

Apr 24th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. package a4_FINAL_EXAM_14March2019;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class P1_TheIsleOfManRace_gr2 {
  8.     public static void main(String[] args) {
  9.         @SuppressWarnings("resource")
  10.  
  11.         Scanner sc = new Scanner(System.in);
  12.  
  13.         boolean isFound = false;
  14.  
  15.         while (!isFound) {
  16.             String line = sc.nextLine();
  17.             String regex = "([$#%*&])(?<name>[A-Za-z]+)\\1=(?<length>[\\d]+)!!(?<geohash>.+)";
  18.             Pattern pattern = Pattern.compile(regex);
  19.             Matcher matcher = pattern.matcher(line);
  20.  
  21.             if (matcher.find()) {
  22.                 String name = matcher.group("name");
  23.                 int length = Integer.parseInt(matcher.group("length"));
  24.                 String geohash = matcher.group("geohash");
  25.  
  26.                 if (length == geohash.length()) {
  27.                     isFound = true;
  28.                     String geohashCode = "";
  29.                     for (int i = 0; i < geohash.length(); i++) {
  30.                         char symbol = geohash.charAt(i);
  31.                         geohashCode += (char)(symbol + geohash.length());
  32.                     }
  33.                     System.out.println(String.format("Coordinates found! %s -> %s", name, geohashCode));
  34.                     isFound = true;
  35.                 } else {
  36.                     isFound = false;
  37.                     System.out.println("Nothing found!");
  38.                 }
  39.             } else {
  40.                 isFound = false;
  41.                 System.out.println("Nothing found!");
  42.             }
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment