Advertisement
meteor4o

JF-Exams-AnimalSanctuary

Aug 2nd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.sql.Array;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class AnimalSanctuary {
  10.     public static void main(String[] args) {
  11.         Scanner sc = new Scanner(System.in);
  12.  
  13.         int n = Integer.parseInt(sc.nextLine());
  14.         String input = "";
  15.         String regex = "^n:(.+);t:(.+);c--([A-Za-z\\s]*)$";
  16. //        String[] filteredInput;
  17.         Pattern pattern = Pattern.compile(regex);
  18.         String name = "";
  19.         String animal = "";
  20.         int totalWeight = 0;
  21.  
  22.         for (int i = 0; i < n; i++) {
  23.             input = sc.nextLine();
  24.             Matcher matcher = pattern.matcher(input);
  25.             if (matcher.find()) {
  26.                 String nameCoded = matcher.group(1);
  27.                 String animalCoded = matcher.group(2);
  28.                 String country = matcher.group(3);
  29.  
  30.                 for (int j = 0; j < animalCoded.length(); j++) {
  31.                     char c = animalCoded.charAt(j);
  32.                     if (Character.isLetter(c)) {
  33.                         animal += c;
  34.                     } else if (Character.isDigit(c)) {
  35.   //                      int weight = c;
  36.                         totalWeight += Character.getNumericValue(c);
  37.                     }
  38.                 }
  39.  
  40.                 for (int j = 0; j < nameCoded.length(); j++) {
  41.                     char c = nameCoded.charAt(j);
  42.                     if (Character.isLetter(c)) {
  43.                         name += c;
  44.                     } else if (Character.isDigit(c)) {
  45.  //                       int weight2 = Integer.valueOf(c);
  46.                         totalWeight += Character.getNumericValue(c);
  47.                     }
  48.                 }
  49.  
  50.                 System.out.printf("%s is a %s from %s%n", name, animal, country);
  51.                 name = "";
  52.                 animal = "";
  53.                 country = "";
  54.             }
  55.         }
  56.         System.out.printf("Total weight of animals: %dKG", totalWeight);
  57.  
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement