Advertisement
mirozspace

Pr1_1404gr2

Apr 24th, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 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 Pr1 {
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scanner = new Scanner(System.in);
  9.         String regex = "^([#*&%])(?<racer>[A-Za-z]+)\\1=(?<lengthGeohashCode>\\d{2,})!!(?<geohashCode>.+)$";
  10.         Pattern pattern = Pattern.compile(regex);
  11.         while (true) {
  12.             String line = scanner.nextLine();
  13.             Matcher matcher = pattern.matcher(line);
  14.             if (matcher.find()) {
  15.                 String racer = matcher.group("racer");
  16.                 int lengthGeohashCode = Integer.parseInt(matcher.group("lengthGeohashCode"));
  17.                 String geohashCode = matcher.group("geohashCode");
  18.  
  19.                 racer = cleanString(racer);
  20.                 geohashCode = increase(geohashCode, lengthGeohashCode);
  21.  
  22.                 System.out.println("Coordinates found! " + racer + " -> " + geohashCode);
  23.                 break;
  24.             } else {
  25.                 System.out.println("Nothing found!");
  26.             }
  27.         }
  28.     }
  29.  
  30.     private static String cleanString(String str) {
  31.         StringBuilder sb = new StringBuilder();
  32.         for (int i = 0; i < str.length(); i++) {
  33.             char symbol = str.charAt(i);
  34.             if (Character.isLetter(symbol) || symbol == ' ') {
  35.                 sb.append(symbol);
  36.             }
  37.         }
  38.         return sb.toString();
  39.     }
  40.  
  41.     private static String increase(String str, int lenght) {
  42.         StringBuilder sb = new StringBuilder();
  43.         for (int i = 0; i < str.length(); i++) {
  44.             char symbol = str.charAt(i);
  45.             symbol = (char) (symbol + lenght);
  46.             sb.append(symbol);
  47.         }
  48.         return sb.toString();
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement