Guest User

Untitled

a guest
Jan 22nd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. String s = "[1,2.5,3],[5,2,3],[1,6,3]";
  2.  
  3. Vector<Vector<Double>> matrix = new Vector<Vector<Double>>();
  4. for(int j=0;j<s.length();j++) {
  5. if (s.charAt(j)=='[') {
  6. int k=s.indexOf("]");
  7. StringTokenizer st = new StringTokenizer(s.substring(j+1, j+k));// j+k-1 does not work either
  8. Vector<Double> vector = new Vector<Double>();
  9. while (st.hasMoreTokens()) {
  10. vector.add(Double.parseDouble(st.nextToken(",")));//Exception in thread "main" java.lang.NumberFormatException: For input string: "3]"
  11. }
  12. matrix.add(vector);
  13. }
  14. }
  15.  
  16. if (s.charAt(j)=='[') {
  17. int k=s.indexOf("]");
  18.  
  19. if (s.charAt(j)=='[') {
  20. int k = s.indexOf("]", j);
  21.  
  22. StringTokenizer st = new StringTokenizer(s.substring(j+1, k));
  23.  
  24. String[] elements = s.substring(j+1, k).split(",");
  25. Vector<Double> vector = new Vector<Double>();
  26. for (String element : elements) {
  27. vector.add(Double.parseDouble(element));
  28. }
  29. matrix.add(vector);
  30.  
  31. while(s.contains("[")) {
  32. String s1 = s.substring(s.indexOf("[")+1, s.indexOf("]"));
  33. if(s1!=null && s1.isEmpty()!=true && s1.contains(",") ) {
  34. String[] sArr = s1.split(",");
  35. for (String string : sArr) {
  36. Double d = Double.valueOf(string);
  37. System.out.println(d);
  38. // put it where you need
  39. }
  40. }
  41. s = s.substring(s.indexOf("]")+1);
  42. }
Add Comment
Please, Sign In to add comment