开发者

tokenizer null character

inputValue = "111,GOOG,20,475.0"

StringTokenizer tempToken = new StringTokenizer(inputValue, ",");

while(tempToken.hasMoreTokens() == true)
{
    test = token.nextToken();
    counterTest++;
}

It's giving me some invalid correct NULL character

I started to learn stringtokenizer and I'm not sure at this point w开发者_高级运维hat wen't wrong logicly I think it works out but am I forgetting something?


I see some typo in your code. However,using StringTokenizer is discouraged in new code. From the javadocs:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

The recommended way is to use String#split. Something like:

private void customSplit(String source) {
    String[] tokens = source.split(";");
    for (int i = 0; i < tokens; i++) {
        System.out.println("Token" + i + "is: " + token[i]);
    }
}


Your code snippet is working with some minor adjustments, maybe your missing something simple, so check the rewritten full example below:

public static void main(String[] args) throws Exception {

    String inputValue = "111,GOOG,20,475.0";

    StringTokenizer tempToken = new StringTokenizer(inputValue, ",");

    int counterTest = 0;
    while (tempToken.hasMoreTokens()) {
        String test = tempToken.nextToken();
        System.out.println(test);

        counterTest++;
    }

    System.out.println("-------------------");
    System.out.println("counterTest = " + counterTest);
}

Output:

111
GOOG
20
475.0
-------------------
counterTest = 4
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜