Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exam;
- import exam.engines.Engine;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // TODO
- // Engine engine = new Engine();
- // engine.run();
- int n = Integer.parseInt(scanner.nextLine());
- Pattern pattern = Pattern.compile("^n:(?<name>[^;]+);t:(?<kind>[^;]+);c--(?<country>[A-Za-z\\s]+)$");
- int totalWeight = 0;
- for (int i = 0; i < n; i++) {
- String input = scanner.nextLine();
- Matcher matcher = pattern.matcher(input);
- StringBuilder nameResult = new StringBuilder();
- StringBuilder kindResult = new StringBuilder();
- int animalWeight = 0;
- if (matcher.find()) {
- String animalName = matcher.group("name");
- String animalKind = matcher.group("kind");
- String country = matcher.group("country");
- for (int j = 0; j < animalName.length(); j++) {
- char currChar = animalName.charAt(j);
- if (Character.isLetter(currChar)) {
- nameResult.append(currChar);
- }
- if (Character.isDigit(currChar)) {
- animalWeight += Character.getNumericValue(currChar);
- }
- }
- for (int j = 0; j < animalKind.length(); j++) {
- char currChar = animalKind.charAt(j);
- if (Character.isLetter(currChar)) {
- kindResult.append(currChar);
- }
- if (Character.isDigit(currChar)) {
- animalWeight += Character.getNumericValue(currChar);
- }
- }
- totalWeight += animalWeight;
- System.out.println(String.format("%s is a %s from %s", nameResult, kindResult, country));
- }
- }
- System.out.printf("Total weight of animals: %dKG\n", totalWeight);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement