LardaX

SumOfAllValues70/100

Feb 6th, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.math.BigDecimal;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. public class SumOfAllValues {
  11.     public static void main(String[] args) throws IOException {
  12.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  13.         String keysRegex = "^(?<startKey>[a-zA-Z_]+)[\\d](.*?[\\d])?(?<endKey>[a-zA-Z_]+)$";
  14.         Pattern keyPattern = Pattern.compile(keysRegex);
  15.  
  16.         String numberRegex = "([\\d]+(\\.\\d+)?(?!\\.))";
  17.         Pattern numberPattern = Pattern.compile(numberRegex);
  18.  
  19.         String keys = reader.readLine();
  20.         String text = reader.readLine();
  21.  
  22.         Matcher matchKeys = keyPattern.matcher(keys);
  23.  
  24.         if (!matchKeys.find()) {
  25.             System.out.println("<p>A key is missing</p>");
  26.             return;
  27.         }
  28.  
  29.         String startKey = matchKeys.group("startKey");
  30.         String endKey = matchKeys.group("endKey");
  31.  
  32.         BigDecimal totalValue = new BigDecimal(0 + "");
  33.  
  34.         Pattern matchPattern = Pattern.compile(startKey + ".*?" + endKey);
  35.         Matcher patternMatch = matchPattern.matcher(text);
  36.         while (patternMatch.find()) {
  37.             String match = patternMatch.group();
  38.             Matcher numbers = numberPattern.matcher(match);
  39.  
  40.             while (numbers.find()) {
  41.                 totalValue = totalValue.add(new BigDecimal(numbers.group() + ""));
  42.             }
  43.         }
  44.  
  45.         if (totalValue.equals("0")) {
  46.             System.out.println("<p>The total value is: <em>nothing</em></p>");
  47.         } else {
  48.             System.out.println(String.format("<p>The total value is: <em>%s</em></p>", totalValue));
  49.         }
  50.  
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment