Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.*;
  3.  
  4. public class AnimalSanctuary {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int n = Integer.parseInt(scanner.nextLine());
  9.  
  10.         int weight = 0;
  11.  
  12.         for (int i = 0; i < n; i++) {
  13.             String data = scanner.nextLine();
  14.             String regex = "^(n:)(.+[^;])(;t:)(.+[^;])(;c--)([ a-zA-Z]+)$";
  15.             Pattern pattern = Pattern.compile(regex);
  16.             Matcher matcher = pattern.matcher(data);
  17.  
  18.             if (matcher.find()){
  19.                 String name = matcher.group(2);
  20.                 String nameRegex = "[a-zA-Z]+";
  21.                 Pattern namePattern = Pattern.compile(nameRegex);
  22.                 Matcher nameMatcher = namePattern.matcher(name);
  23.  
  24.                 String nameResult = "";
  25.                 while (nameMatcher.find()){
  26.                     nameResult += nameMatcher.group();
  27.                 }
  28.  
  29.                 String kind = matcher.group(4);
  30.                 String kindResult = "";
  31.                 Matcher kindMatcher = namePattern.matcher(kind);
  32.  
  33.                 while (kindMatcher.find()){
  34.                     kindResult += kindMatcher.group();
  35.                 }
  36.  
  37.                 String country = matcher.group(6);
  38.  
  39.                 String weightRegex = "([0-9])";
  40.                 Pattern weightPattern = Pattern.compile(weightRegex);
  41.                 Matcher weightName = weightPattern.matcher(name);
  42.  
  43.                 while(weightName.find()){
  44.                     weight += Integer.parseInt(weightName.group());
  45.                 }
  46.  
  47.                 Matcher weightKind = weightPattern.matcher(kind);
  48.  
  49.                 while (weightKind.find()){
  50.                     weight += Integer.parseInt(weightKind.group());
  51.                 }
  52.  
  53.                 System.out.printf("%s is a %s from %s%n", nameResult, kindResult, country);
  54.  
  55.             }
  56.         }
  57.  
  58.         System.out.printf("Total weight of animals: %dKG", weight);
  59.  
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement