alternatives to Scanner?
I have written a program that runs fine. It has several subroutines, each of which runs fine (each uses scanner). The problem comes when I try to call one subroutine after another; it fails because of a scanner mismatch error. I can't figure out how to flush the scanner. If I add scan.next() to flush the buffer the program hangs waiting input. So I would like to know what my options are.
1. Write my own scanner class using buffered input stream?
2. Are there any other scanner type classes I can use?
3. I tried using scan.close(); per API, but that also terminated my program prematurely. I even tried declaring scan as new Scanner in each subroutine, and then scan.close() at end of the routine, but it also closed my entire program.
I'm resigned to writing my own input class, hoped there was an alternative out there.
I would fix the bugs in the program. To do that you'll need to find them first.
By 'flush the Scanner', do you mean you want to throw away whatever the user has entered that your program hasn't read yet?
Perhaps a combo of hasNext to test if there is anything in the buffer and next to pull in out.
Can you post some code that shows the mismatch error? Would a has..... method to test what is in the buffer keep that from happening?
You were both right. Norm, your idea to use hasNext to track down the stray input worked great.