Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class AnimalSanctury {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int nLines = Integer.parseInt(reader.readLine());
- int totalWeight = 0;
- for (int i = 0; i < nLines; i++) {
- String line = reader.readLine();
- Pattern pattern = Pattern.compile("n:([^;]+);t:([^;]+);c--([^;]+)[\\w\\s]*");
- Matcher matcher = pattern.matcher(line);
- String animalName = "";
- String animalKind = "";
- String country = "";
- // ако линията е с валидни данни matcher ще има групи
- if (matcher.find()) {
- // input -> n:M5%ar4#le@y;t:B3#e!!a2#2r;c--Australia
- //
- // group 1 -> M5%ar4#le@y
- // group 2 -> B3#e!!a2#2r
- // group 3 -> Australia
- String nameConstruct = matcher.group(1); // -> Взима group 1
- // итерираме по стринга за да вземем само буквите в него
- for (int j = 0; j < nameConstruct.length(); j++) { // php -> strlen
- if (Character.isLetter(nameConstruct.charAt(j))) { // проверка дали символа е буква -> php -> nameConstruct[$i]
- animalName += nameConstruct.charAt(j); // -> Добавяме буквата към името
- }
- if (Character.isDigit(nameConstruct.charAt(j))) { // ако символа е число го добавяме към променливата за теглото, което трябва да принтираме накрая
- totalWeight += Integer.parseInt(String.valueOf(nameConstruct.charAt(j))); // парсваме символа към int -> В php Едва ли е нужно :)
- }
- }
- String kindAnimalConstruct = matcher.group(2);
- for (int j = 0; j < kindAnimalConstruct.length(); j++) {
- if (Character.isLetter(kindAnimalConstruct.charAt(j))) { // проверка дали символа е буква -> php -> kindAnimalConstruct[$i]
- animalKind += kindAnimalConstruct.charAt(j); // -> Добавяме буквата към името
- }
- if (Character.isDigit(kindAnimalConstruct.charAt(j))) { // ако символа е число го добавяме към променливата за теглото, което трябва да принтираме накрая
- totalWeight += Integer.parseInt(String.valueOf(kindAnimalConstruct.charAt(j))); // парсваме символа към int -> В php Едва ли е нужно :)
- }
- }
- country = matcher.group(3); // Задаваме country на трета група от Matcher-a
- System.out.printf("%s is a %s from %s%n", animalName, animalKind, country);
- }
- }
- System.out.printf("Total weight of all animals: %dKG", totalWeight);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement