SHARE
TWEET

Arriving in Kathmandu

StefanShivarov Dec 6th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class arriving_in_Kathmandu {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String input = scanner.nextLine();
  12.         String regex = "^(?<name>([!?@#$]*[A-z]*[0-9]*)*)=(?<length>\\d+)<<(?<coordinates>.*)$";
  13.         Pattern pattern = Pattern.compile(regex);
  14.  
  15.         while(!input.equals("Last note")){
  16.  
  17.             Matcher matcher = pattern.matcher(input);
  18.  
  19.             if(matcher.find()){
  20.  
  21.                 String nameRegex = matcher.group("name");
  22.                 int length = Integer.parseInt(matcher.group("length"));
  23.                 String coordinates = matcher.group("coordinates");
  24.  
  25.                 if(coordinates.length()==length){
  26.  
  27.                     StringBuilder name = new StringBuilder();
  28.  
  29.                     for(int i = 0; i < nameRegex.length(); i++){
  30.  
  31.                         if(Character.isLetter(nameRegex.charAt(i))){
  32.  
  33.                             name.append(nameRegex.charAt(i));
  34.                         }
  35.  
  36.                     }
  37.  
  38.                     System.out.println(String.format("Coordinates found! %s -> %s", name, coordinates));
  39.  
  40.                 }else{
  41.  
  42.                     System.out.println("Nothing found!");
  43.                 }
  44.  
  45.             }else{
  46.  
  47.                 System.out.println("Nothing found!");
  48.  
  49.             }
  50.  
  51.             input = scanner.nextLine();
  52.         }
  53.  
  54.  
  55.     }
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top