The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Alright, so what I'm trying to do is import a .txt file, use it as a scanner, and then have it search for the number of times the input word is used inside of the .txt file. That part works. I also want it to count the total number of words in the file. That part doesn't work and I can't seem to figure out why, especially as that should be the easier part of this. I'm very new to java, please help me!

EDIT: It returns "x", so it is reaching that part of the code, but it's like it is already at the end of the text file so there isn't a chance for it to go through the while method. Is there a way I can reset the scanner to start from the beginning of the file again? Or am I completely off about that?

Re: Using the same scanner multiple times in code help

Originally Posted by jps

I think adding a couple of println statements to the code so you can see what it is doing will help you figure out how to make it do what you want it to do.

Thanks for the suggestion. It looks like it must be at the end of the file, because I can get it to print out whatever number I want before the "while (words.hasNext)" statements. I've figured out what it's doing, but now I'm stuck on trying to get it to do what I want to do. Is there a way I can import my text file and then create multiple scanners out of it? Or anyway that I can reset the scanner to the beginning after it goes through each method? Thanks for the help.