Advertisement
Guest User

Untitled

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