Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. package bg.softuni.tech.exams;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class AnimalSanctuary {
  8.  
  9.     public static void main(String[] args) {
  10.         try (Scanner sc = new Scanner(System.in)) {
  11.             int n = Integer.parseInt(sc.nextLine());
  12.             String name = null;
  13.             String kind = null;
  14.             String country = null;
  15.             int totalWeight = 0;
  16.             for (int i = 0; i < n; i++) {
  17.                 String line = sc.nextLine();
  18.                 Pattern pattern = Pattern.compile("n:.+;t:.+;c--[A-Z][a-z]+");
  19.                 Matcher matcher = pattern.matcher(line);
  20.                 while (matcher.find()) {
  21.                     Pattern stringPattern = Pattern.compile("(?!n:|t:|c--)[A-Za-z ;]");
  22.                     Matcher stringMatcher = stringPattern.matcher(line);
  23.                     StringBuilder sb = new StringBuilder();
  24.                     while (stringMatcher.find()) {
  25.                         sb.append(stringMatcher.group());
  26.                     }
  27.                     String[] animalFeatures = sb.toString().split(";");
  28.                     name = animalFeatures[0];
  29.                     kind = animalFeatures[1];
  30.                     country = animalFeatures[2];
  31.                     Pattern weightPattern = Pattern.compile("\\d");
  32.                     Matcher weightMatcher = weightPattern.matcher(line);
  33.                     while (weightMatcher.find()) {
  34.                         totalWeight += Integer.parseInt(weightMatcher.group());
  35.                     }
  36.                     System.out.println(String.format("%s is a %s from %s", name, kind, country));
  37.                 }
  38.             }
  39.             System.out.println(String.format("Total weight of animals: %dKG", totalWeight));
  40.         }
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement