How to avoid resetting the java Scanner position
Posted
by
Derek
on Stack Overflow
See other posts from Stack Overflow
or by Derek
Published on 2012-04-03T23:07:43Z
Indexed on
2012/04/03
23:28 UTC
Read the original article
Hit count: 218
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 regext 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 diubg sine debugging, it appears that there is a HearCharBuffer 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
© Stack Overflow or respective owner