Advertisement
desislava_topuzakova

2. Destination Mapper

Mar 20th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. package ExamPrep;
  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 DestinationMapper_02 {
  10. public static void main(String[] args) {
  11. Scanner scanner = new Scanner(System.in);
  12. String allDestinations = scanner.nextLine();
  13. //"=Hawai=/Cyprus/=Invalid/invalid==i5valid=/I5valid/=i="
  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. //matcher = ["=Hawai=", "/Cyprus/"]
  20.  
  21. List<String> validDestinations = new ArrayList<>();
  22. while (matcher.find()) {
  23. //matcher.group -> "=Hawai="
  24. validDestinations.add(matcher.group("destination"));
  25. }
  26. System.out.println("Destinations: " + String.join(", ", validDestinations));
  27.  
  28. int travelPoints = 0;
  29. for (String destination : validDestinations) {
  30. travelPoints += destination.length();
  31. }
  32. System.out.println("Travel Points: " + travelPoints);
  33. }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement