Ivelin_1936

Letter Expresion

Jun 9th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.math.BigDecimal;
  5. import java.text.DecimalFormat;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class LetterExpresion {
  10.  
  11.     private static final String PATTERN_FLOATING_POINT_NUMBERS = "(?<symbols>\\D+)?(?<number>\\d+([.]?\\d+)?)";
  12.     private static final String PATTERN_DECIMAL_NUMBERS = "(?<symbols>\\D+)?(?<number>\\d+)";
  13.     private static final String GROUP_OF_SYMBOLS = "symbols";
  14.     private static final String GROUP_OF_NUMBER = "number";
  15.  
  16.     public static void main(String[] args) throws IOException {
  17.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  18.  
  19.         DecimalFormat df = new DecimalFormat("0.#######");
  20.  
  21.         String inputLine = reader.readLine();
  22.         Pattern pattern = Pattern.compile(PATTERN_DECIMAL_NUMBERS);
  23.         Matcher matcher = pattern.matcher(inputLine);
  24.  
  25.         BigDecimal sum = BigDecimal.ZERO;
  26.         while (matcher.find()) {
  27.             String symbol = matcher.group(GROUP_OF_SYMBOLS);
  28.  
  29.             int symbolLength = 0;
  30.             if (symbol != null) {
  31.                 symbolLength = symbol.length();
  32.             }
  33.  
  34.             BigDecimal number = new BigDecimal(matcher.group(GROUP_OF_NUMBER));
  35.             if (symbolLength % 2 == 0) {
  36.               sum = sum.add(number);
  37.               continue;
  38.             }
  39.             sum = sum.subtract(number);
  40.         }
  41.         System.out.println(df.format(sum));
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment