If I want to copy the content of a text file using a string tokenizer let’s say like this:
StringTokenizer strk = new StringTokenizer(new String(buffer), Constant.DELIMS,true);
And then I want to browse the string tokenizer but also keep count in a variable of how many lines I passed from the initial file:(by this I mean count how many tokens that represent a newline character)
nr_lines=0;
endline="\n";
while (strk.hasMoreTokens()) {
String word = strk.nextToken();
if (word.equals(endline)) nr_lines++;
}
But it doesn’t seem to work right. Am I making the comparison with a wrong delimiter(\n). I also tried with \r. But it returns larger numbers than it should, like nr_lines would increment when it shouldn’t. Could you please help me out. Also if you have another idea of how I could keep track of the line number after transferring the content of a file to a string tokenizer, it would be most welcomed. Thanks a lot.
I would recommend using
Scannerinstead. While it isn’t as useful for returningObjects, it has thenextLinemethod, which you can then parse through usingStringTokenizer.Like this: