Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bg.softuni.tech.exams;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class AnimalSanctuary {
- public static void main(String[] args) {
- try (Scanner sc = new Scanner(System.in)) {
- int n = Integer.parseInt(sc.nextLine());
- String name = null;
- String kind = null;
- String country = null;
- int totalWeight = 0;
- for (int i = 0; i < n; i++) {
- String line = sc.nextLine();
- Pattern pattern = Pattern.compile("n:.+;t:.+;c--[A-Z][a-z]+");
- Matcher matcher = pattern.matcher(line);
- while (matcher.find()) {
- Pattern stringPattern = Pattern.compile("(?!n:|t:|c--)[A-Za-z ;]");
- Matcher stringMatcher = stringPattern.matcher(line);
- StringBuilder sb = new StringBuilder();
- while (stringMatcher.find()) {
- sb.append(stringMatcher.group());
- }
- String[] animalFeatures = sb.toString().split(";");
- name = animalFeatures[0];
- kind = animalFeatures[1];
- country = animalFeatures[2];
- Pattern weightPattern = Pattern.compile("\\d");
- Matcher weightMatcher = weightPattern.matcher(line);
- while (weightMatcher.find()) {
- totalWeight += Integer.parseInt(weightMatcher.group());
- }
- System.out.println(String.format("%s is a %s from %s", name, kind, country));
- }
- }
- System.out.println(String.format("Total weight of animals: %dKG", totalWeight));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement