Advertisement
dimipan80

Java Regex: Sum of All Values

Aug 3rd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. /*
  2.  * You are given a keys string and a text string.
  3.  * Write a program that finds the start key and the end key from the keys string and then applies them to the text string.
  4.  * The start key will always stay at the beginning of the keys string.
  5.  * It can contain only letters and underscore and ends just before the first found digit.
  6.  * The end key will always stay at the end of the keys string.
  7.  * It can contain only letters and underscore and starts just after the last found digit.
  8.  * Print at the console the sum of all values (only integer and floating-point numbers with dot as a separator are considered valid)
  9.  *   in the text string, between a start key and an end key. If the value is 0 then print “The total value is: nothing”.
  10.  * If no start key or no end key is found, then print “A key is missing”.
  11.  * The Input will be read from the console.
  12.  * The first line will hold the keys string and the second line will hold the text to search.
  13.  * The output should hold the result text, printed in an HTML paragraph. The actual value of the sum should be italic.
  14.  * The number should been 2 digits rounded after decimal separator.
  15.  */
  16.  
  17.  
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.util.Locale;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;
  24.  
  25. public class SumOfAllValues {
  26.     public static void main(String[] args) {
  27.         Locale.setDefault(Locale.ROOT);
  28.         String keysStr = "";
  29.         String text = "";
  30.  
  31.         try {
  32.             BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  33.  
  34.             keysStr = reader.readLine();
  35.             text = reader.readLine();
  36.         } catch (IOException e) {
  37.             e.printStackTrace();
  38.         }
  39.  
  40.         Pattern keysPattern = Pattern.compile("^([a-zA-Z_]+)\\d.*\\d([a-zA-Z_]+)");
  41.         Matcher keysMatcher = keysPattern.matcher(keysStr);
  42.         if (!keysMatcher.find()) {
  43.             System.out.println("<p>A key is missing</p>");
  44.             return;
  45.         }
  46.  
  47.         String startKey = keysMatcher.group(1);
  48.         String endKey = keysMatcher.group(2);
  49.  
  50.         Pattern valuesPattern = Pattern
  51.                 .compile(Pattern.quote(startKey) + "([\\d.]+)" + Pattern.quote(endKey));
  52.  
  53.         Matcher valuesMatcher = valuesPattern.matcher(text);
  54.         double valuesSum = 0;
  55.         while (valuesMatcher.find()) {
  56.             valuesSum += Double.parseDouble(valuesMatcher.group(1));
  57.         }
  58.  
  59.         if (valuesSum != 0) {
  60.             if (valuesSum == (int) valuesSum) {
  61.                 System.out.printf("<p>The total value is: <em>%d</em></p>", (int) valuesSum);
  62.             } else {
  63.                 System.out.printf("<p>The total value is: <em>%.2f</em></p>", valuesSum);
  64.             }
  65.         } else {
  66.             System.out.println("<p>The total value is: <em>nothing</em></p>");
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement