Hi,

This code snippet works. Except for one line, which I cannot find the solution to.

public void read(Scanner in)
{
Pattern p = in.delimiter();
in.useDelimiter("\n");
while(in.hasNext())
{
in.next();
lineCount++;
}

//rewind pointer - how!! ??

in.useDelimiter(p);
while(in.hasNext())
{
wordCount += 1;
charCount += in.next().length();
}
}

...it appears, much to my chagrin, that the Scanner class *DOES NOT* have a "rewind" method to set the (stinking) pointer back to the beginning. Because I can't resolve this issue, for a text input stream, I either get a perfect line count, or a perfect character/word count - but not both.

...I must do it this way because the calling class will only give me a Scanner object.
Is there any way at all, to reset the Scanner object's data pointer so that I can get both line counts, *and* char/word counts?

Thanx in advance to anyone who replies...

Slowly

You probably already know if you had the File object, you could recreate the Scanner and that would reset the pointer to the beginning of the file. But since you don't have that, you need to rethink your loops. Can you think of a way to combine both checking for words and checking for the end of a line inside a single loop?

Hint from the javadoc: in.next() finds and returns the next complete token from the scanner.

You could read a whole line into a String and then create a Scanner to read individual words from that String. To rewind back to the beginning of the String, create a new Scanner with that String again.

Yep, NormR1, that's how I finally figured it out. An extraodrinarily lame workaround, but it worked on paper. Wiley Plus' labrat choked on it tho. If I could write a custom data scrubbing class, ya kramerd I'd use the File object...What I'd like to eventually do is write a generic input data scrubbing class for any data stream - I wonder if something like that has already been written...?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.