简体   繁体   中英

stringtokenizer returns wrong tokens

I want to "parse" a given string into vectors. A vector starts with "[" and ends with "]". The values of the vector as well as the vectors themself are seperated by ",". If I use integers as values my code works fine "[1,2,3],[5,2,3],[1,6,3]". But when I mix integer values with double values "[1, 2.5 ,3],[5,2,3],[1,6,3]" stringtokenizer returns wrong values (in this case "1" "2.5" but then " 3] " ......)

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("]"); 

Finds the index of the first occurrence of ] in s , starting from the beginning of the string. What you really want is to find the first occurrence after the start of current vector :

if (s.charAt(j)=='[') {
    int k = s.indexOf("]", j);

The reason it works when you just have 2 instead of 2.5 is that the number of characters in each vector just happened to be the same, so taking the fist occurrence of ] to calculate the length of the vector worked by luck.

Note, you will also have to change the end index of your substring call to k :

StringTokenizer st = new StringTokenizer(s.substring(j+1, k));

As a side note, use of StringTokenizer is not recommended. In this case you should be using split() instead:

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);

Vector and StringTokenizer ? :) cute!

    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);
    }

不推荐使用StringTokenizer,你应该使用字符串split()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM