fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.regex.*;
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String rx = "([0-9]+)>";
  13. String text = "<D,E> 200 200 799 1220 No [<805,1380,Voltage,3,2>]\n"+
  14. "<A,C> 300 300 415 1230 Yes [<417,1340,Voltage,3,0><415,1230,Current,3,1>]\n"+
  15. "<D,B> 200 200 799 122 No [<80,137,Voltage,3,2>]";
  16. Pattern p = Pattern.compile(rx);
  17. List<String> res = new ArrayList<>();
  18.  
  19. BufferedReader reader = new BufferedReader(new StringReader(text));
  20. String line = reader.readLine();
  21. while (line != null) {
  22. String[] chunks = line.split("\\s+");
  23. if (chunks.length > 2) { // chunks[2] = 200 / 300 values
  24. Matcher m = p.matcher(line); // Find all numbers at the end of >
  25. int val = 0;
  26. while (m.find()) {
  27. val += Integer.parseInt(m.group(1));
  28. }
  29. res.add(chunks[2]);
  30. res.add(Integer.toString(val));
  31. }
  32. //result.add(line);
  33. line = reader.readLine();
  34. }
  35.  
  36. System.out.println(res);
  37. }
  38. }
Success #stdin #stdout 0.04s 320576KB
stdin
Standard input is empty
stdout
[200, 2, 300, 1, 200, 2]