thufir

Untitled

Dec 18th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package regex;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import static java.lang.System.out;
  8.  
  9. public class Ratios {
  10.  
  11.     private static String line = "HP: 272/272  CP: 204/529  ADRENALINE: 105 ENDORPHINE: 105  BERSERK: 0 None: 0%";
  12.     private static Map<String, String> stats = new HashMap<>();
  13.     private static Map<String, String> ratios = new HashMap<>();
  14.  
  15.     public static void main(String[] args) {
  16.         out.println(line);
  17.         populateMap();
  18.         ratios();
  19.     }
  20.  
  21.     private static void populateMap() {
  22.         Pattern pattern = Pattern.compile("(\\w+): +(\\S+)");
  23.         Matcher matcher = pattern.matcher(line);
  24.         while (matcher.find()) {
  25.             stats.put(matcher.group(1), matcher.group(2));
  26.         }
  27.     }
  28.  
  29.     private static void printMap() {
  30.         for (Map.Entry<String, String> e : stats.entrySet()) {
  31.             String key = e.getKey();
  32.             String val = e.getValue();
  33.             out.println(key + "\t\t" + val);
  34.         }
  35.     }
  36.  
  37.     private static void ratios() {
  38.         Pattern pattern = Pattern.compile("(\\d+)\\/(\\d+)");
  39.         Matcher matcher;
  40.         String numerator = "", denominator = "";
  41.         for (Map.Entry<String, String> e : stats.entrySet()) {
  42.             out.println(e.getKey());
  43.             matcher = pattern.matcher(e.getValue());
  44.             while (matcher.find()) {
  45.                 out.println(matcher.group(2) + "\t" + numerator + "\t" + denominator);
  46.             }
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment