Advertisement
veronikaaa86

02. Destination Mapper

Jul 30th, 2022
1,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. package examPrep2;
  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 P02DestinationMapper {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         String inputLine = scanner.nextLine();
  14.  
  15.         Pattern pattern = Pattern.compile("([=\\/])(?<destination>[A-Z][A-Za-z]{2,})\\1");
  16.         Matcher matcher = pattern.matcher(inputLine);
  17.  
  18.         int travelPoints = 0;
  19.         List<String> destinationList = new ArrayList<>();
  20.         while (matcher.find()) {
  21.             String destination = matcher.group("destination");
  22.  
  23.             travelPoints = travelPoints + destination.length();
  24.  
  25.             destinationList.add(destination);
  26.         }
  27.  
  28.         System.out.print("Destinations: ");
  29.         System.out.println(String.join(", ", destinationList));
  30.         System.out.printf("Travel Points: %d%n", travelPoints);
  31.     }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement