Advertisement
desislava_topuzakova

2. Destination Mapper

Jul 22nd, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. package exam_prepration;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class Task_02 {
  10. public static void main(String[] args) {
  11. Scanner scanner = new Scanner(System.in);
  12.  
  13. String allDestinations = scanner.nextLine(); //всички дестинации
  14.  
  15. String regex = "([=\\/])(?<destination>[A-Z][a-zA-Z]{2,})\\1";
  16. Pattern pattern = Pattern.compile(regex);
  17. Matcher matcher = pattern.matcher(allDestinations);
  18. //съвкупност от текстовете, които отговарят на шаблона -> валидни дестинации
  19. //"=Hawai=/Cyprus/=Invalid/invalid==i5valid=/I5valid/=i="
  20. //matcher = ["=Hawai=", "/Cyprus/"]
  21.  
  22. List<String> validDestinations = new ArrayList<>();
  23. while(matcher.find()) {
  24. //matcher.group -> "=Hawai=" -> "destination" -> "Hawai"
  25. String destination = matcher.group("destination");
  26. validDestinations.add(destination);
  27. }
  28.  
  29. System.out.println("Destinations: " + String.join(", ", validDestinations));
  30.  
  31. int travelPoints = 0;
  32. for (String destination : validDestinations) {
  33. travelPoints += destination.length();
  34. }
  35.  
  36. System.out.println("Travel Points: " + travelPoints);
  37.  
  38.  
  39.  
  40.  
  41.  
  42. }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement