Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.regex.*;
- public class AnimalSanctuary {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- int weight = 0;
- for (int i = 0; i < n; i++) {
- String data = scanner.nextLine();
- String regex = "^(n:)(.+[^;])(;t:)(.+[^;])(;c--)([ a-zA-Z]+)$";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(data);
- if (matcher.find()){
- String name = matcher.group(2);
- String nameRegex = "[a-zA-Z]+";
- Pattern namePattern = Pattern.compile(nameRegex);
- Matcher nameMatcher = namePattern.matcher(name);
- String nameResult = "";
- while (nameMatcher.find()){
- nameResult += nameMatcher.group();
- }
- String kind = matcher.group(4);
- String kindResult = "";
- Matcher kindMatcher = namePattern.matcher(kind);
- while (kindMatcher.find()){
- kindResult += kindMatcher.group();
- }
- String country = matcher.group(6);
- String weightRegex = "([0-9])";
- Pattern weightPattern = Pattern.compile(weightRegex);
- Matcher weightName = weightPattern.matcher(name);
- while(weightName.find()){
- weight += Integer.parseInt(weightName.group());
- }
- Matcher weightKind = weightPattern.matcher(kind);
- while (weightKind.find()){
- weight += Integer.parseInt(weightKind.group());
- }
- System.out.printf("%s is a %s from %s%n", nameResult, kindResult, country);
- }
- }
- System.out.printf("Total weight of animals: %dKG", weight);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement