Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.sql.Array;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class AnimalSanctuary {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = Integer.parseInt(sc.nextLine());
- String input = "";
- String regex = "^n:(.+);t:(.+);c--([A-Za-z\\s]*)$";
- // String[] filteredInput;
- Pattern pattern = Pattern.compile(regex);
- String name = "";
- String animal = "";
- int totalWeight = 0;
- for (int i = 0; i < n; i++) {
- input = sc.nextLine();
- Matcher matcher = pattern.matcher(input);
- if (matcher.find()) {
- String nameCoded = matcher.group(1);
- String animalCoded = matcher.group(2);
- String country = matcher.group(3);
- for (int j = 0; j < animalCoded.length(); j++) {
- char c = animalCoded.charAt(j);
- if (Character.isLetter(c)) {
- animal += c;
- } else if (Character.isDigit(c)) {
- // int weight = c;
- totalWeight += Character.getNumericValue(c);
- }
- }
- for (int j = 0; j < nameCoded.length(); j++) {
- char c = nameCoded.charAt(j);
- if (Character.isLetter(c)) {
- name += c;
- } else if (Character.isDigit(c)) {
- // int weight2 = Integer.valueOf(c);
- totalWeight += Character.getNumericValue(c);
- }
- }
- System.out.printf("%s is a %s from %s%n", name, animal, country);
- name = "";
- animal = "";
- country = "";
- }
- }
- System.out.printf("Total weight of animals: %dKG", totalWeight);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement