I have some code that looks more or less like this:
while(scanner.hasNext())
{
if(scanner.findInLine("Test") !=null) {
//do some things
}else{
scanner.nextLine();
}
}
I am using this to parse an ~10MB text file. The problem is, if I put a breakpoint on the while() and the scanner.nextLine(), I can see that sometimes the scanners position (in the debug window) goes back to zero. I think this is causing me some kind of loop blow up, because the regex in findInLine() starts at zero, looks through some amount of text, advancing the position, and then it randomly gets set back to zero, so it has to re-parse all that text again.
Any ideas what can be causing that? Am I even doing this the right way?
Thanks
Some additional info:
The Scanner is instantiated from an InputStream. After debugging, it appears that there is a HeapCharBuffer that Scanner uses and it only allows 1024 characters at a time, and then resets. Is there a way to avoid this, or do things differently? That seems like a small amount of characters to be able to scan.
Derek
You're mixing Scanner.hasNext()
and Scanner.nextLine()
. Don't do that; they handle tokenization differently.
Use hasNext()
with next()
or hasNextLine()
with nextLine()