Advertisement
ilianrusev

Technology Fundamentals Final Exam -14 April 2019 Group I- 1

Apr 14th, 2019
131
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. String input = "";
  13. String regex = "^(?<name>[A-Za-z\\d!?@#$]+)=(?<length>[\\d]+)<<(?<code>.+)$";
  14. Pattern pattern = Pattern.compile(regex);
  15.  
  16. System.out.println();
  17.  
  18.  
  19. while (!"Last note".equals(input = scanner.nextLine())) {
  20.  
  21. Matcher matcher = pattern.matcher(input);
  22. if (matcher.find()) {
  23. String name = matcher.group("name");
  24. int length = Integer.parseInt(matcher.group("length"));
  25. String code = matcher.group("code");
  26.  
  27. if (length != code.length()) {
  28. System.out.println("Nothing found!");
  29. } else {
  30. String[] name2 = name.split("[!@#$?]");
  31. String mountain = "";
  32. for (int i = 0; i < name2.length; i++) {
  33. mountain += name2[i];
  34. }
  35. System.out.printf("Coordinates found! %s -> %s\n",mountain, code);
  36. }
  37.  
  38. }else {
  39. System.out.println("Nothing found!");
  40. }
  41. }
  42. }
  43. }
Advertisement
RAW Paste Data Copied
Advertisement