Advertisement
damesova

Arriving in Kathmandu

Apr 15th, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class _01_Task {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         String reg = "^(?<name>[a-zA-Z0-9!@#$?]+)=(?<length>[0-9]+)<<(?<code>.+)$";
  10.         Pattern p = Pattern.compile(reg);
  11.  
  12.         String input = "";
  13.  
  14.         while(!"Last note".equals(input = sc.nextLine())){
  15.             Matcher m = p.matcher(input);
  16.  
  17.             if (m.find()){
  18.                 String name = m.group("name");
  19.                 String len = m.group("length");
  20.                 int checkLength = Integer.parseInt(len);
  21.                 String checkCode = m.group("code");
  22.                 if(checkLength == checkCode.length()){
  23.                     StringBuilder sb = new StringBuilder();
  24.                     for (int i = 0; i < m.group("name").length(); i++) {
  25.                         if(Character.isLetterOrDigit(name.charAt(i))){
  26.                             sb.append(name.charAt(i));
  27.                         }
  28.                     }
  29.                     System.out.printf("Coordinates found! %s -> %s%n", sb, checkCode);
  30.                 }else{
  31.                     System.out.println("Nothing found!");
  32.                 }
  33.             }else{
  34.                 System.out.println("Nothing found!");
  35.             }
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement