Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String s = "[1,2.5,3],[5,2,3],[1,6,3]";
- Vector<Vector<Double>> matrix = new Vector<Vector<Double>>();
- for(int j=0;j<s.length();j++) {
- if (s.charAt(j)=='[') {
- int k=s.indexOf("]");
- StringTokenizer st = new StringTokenizer(s.substring(j+1, j+k));// j+k-1 does not work either
- Vector<Double> vector = new Vector<Double>();
- while (st.hasMoreTokens()) {
- vector.add(Double.parseDouble(st.nextToken(",")));//Exception in thread "main" java.lang.NumberFormatException: For input string: "3]"
- }
- matrix.add(vector);
- }
- }
- if (s.charAt(j)=='[') {
- int k=s.indexOf("]");
- if (s.charAt(j)=='[') {
- int k = s.indexOf("]", j);
- StringTokenizer st = new StringTokenizer(s.substring(j+1, k));
- String[] elements = s.substring(j+1, k).split(",");
- Vector<Double> vector = new Vector<Double>();
- for (String element : elements) {
- vector.add(Double.parseDouble(element));
- }
- matrix.add(vector);
- while(s.contains("[")) {
- String s1 = s.substring(s.indexOf("[")+1, s.indexOf("]"));
- if(s1!=null && s1.isEmpty()!=true && s1.contains(",") ) {
- String[] sArr = s1.split(",");
- for (String string : sArr) {
- Double d = Double.valueOf(string);
- System.out.println(d);
- // put it where you need
- }
- }
- s = s.substring(s.indexOf("]")+1);
- }
Add Comment
Please, Sign In to add comment