How to avoid resetting the java Scanner position

Go To StackoverFlow.com

-1

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

2012-04-03 23:07
by Derek
What does the input look like? How was the scanner instantiated (what is the delimiter)? Please don't post code that "more or less like this" -- post the actual code - Jim Garrison 2012-04-03 23:16
The scanner is instantiated off of an InputStream. The input is just normal ascii text. The delimiter is not changed, and shouldnt matter for these functions in particular - Derek 2012-04-03 23:23
Regardless, you MUST post some sample input that causes the failure - Jim Garrison 2012-04-04 00:39
What is "dibug sine" debugging? What is a HearCharBuffer - Jim Garrison 2012-04-04 00:46
They were clearly typos. Brian solved my problem, no sample data needed - Derek 2012-04-04 15:08


4

You're mixing Scanner.hasNext() and Scanner.nextLine(). Don't do that; they handle tokenization differently.

Use hasNext() with next() or hasNextLine() with nextLine()

2012-04-04 00:45
by Brian Roach
Thank you for the correct answer. I am not sure why this question got downvoted. Haters gon' hate I gues - Derek 2012-04-04 15:07
Ads