Advertisement
emodev

Untitled

Apr 18th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class AnimalSanctury {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         int nLines = Integer.parseInt(reader.readLine());
  12.  
  13.         int totalWeight = 0;
  14.  
  15.  
  16.         for (int i = 0; i < nLines; i++) {
  17.  
  18.             String line = reader.readLine();
  19.  
  20.             Pattern pattern = Pattern.compile("n:([^;]+);t:([^;]+);c--([^;]+)[\\w\\s]*");
  21.  
  22.             Matcher matcher = pattern.matcher(line);
  23.  
  24.  
  25.             String animalName = "";
  26.             String animalKind = "";
  27.             String country = "";
  28.  
  29.  
  30. //            ако линията е с валидни данни matcher ще има групи
  31.             if (matcher.find()) {
  32. //                input -> n:M5%ar4#le@y;t:B3#e!!a2#2r;c--Australia
  33. //
  34. //                 group 1 ->   M5%ar4#le@y
  35. //                 group 2 ->   B3#e!!a2#2r
  36. //                 group 3 ->   Australia
  37.  
  38.                 String nameConstruct = matcher.group(1); // -> Взима group 1
  39.  
  40. //                итерираме по стринга за да вземем само буквите в него
  41.                 for (int j = 0; j < nameConstruct.length(); j++) { // php -> strlen
  42.                     if (Character.isLetter(nameConstruct.charAt(j))) { // проверка дали символа е буква -> php -> nameConstruct[$i]
  43.                         animalName += nameConstruct.charAt(j); // -> Добавяме буквата към името
  44.                     }
  45.                     if (Character.isDigit(nameConstruct.charAt(j))) { // ако символа е число го добавяме към променливата за теглото, което трябва да принтираме накрая
  46.                         totalWeight += Integer.parseInt(String.valueOf(nameConstruct.charAt(j))); // парсваме символа към int -> В php Едва ли е нужно :)
  47.                     }
  48.                 }
  49.  
  50.                 String kindAnimalConstruct = matcher.group(2);
  51.  
  52.                 for (int j = 0; j < kindAnimalConstruct.length(); j++) {
  53.                     if (Character.isLetter(kindAnimalConstruct.charAt(j))) { // проверка дали символа е буква -> php -> kindAnimalConstruct[$i]
  54.                         animalKind += kindAnimalConstruct.charAt(j); // -> Добавяме буквата към името
  55.                     }
  56.                     if (Character.isDigit(kindAnimalConstruct.charAt(j))) { // ако символа е число го добавяме към променливата за теглото, което трябва да принтираме накрая
  57.                         totalWeight += Integer.parseInt(String.valueOf(kindAnimalConstruct.charAt(j))); // парсваме символа към int -> В php Едва ли е нужно :)
  58.                     }
  59.                 }
  60.  
  61.                 country = matcher.group(3); // Задаваме country на трета група от Matcher-a
  62.  
  63.  
  64.             System.out.printf("%s is a %s from %s%n", animalName, animalKind, country);
  65.             }
  66.  
  67.  
  68.         }
  69.  
  70.  
  71.         System.out.printf("Total weight of all animals: %dKG", totalWeight);
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement