Results 1 to 4 of 4
  1. #1
    graympa is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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?

  4. #4
    graympa is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    You were both right. Norm, your idea to use hasNext to track down the stray input worked great.

Similar Threads

  1. Using scanner for CSV
    By getName() in forum Advanced Java
    Replies: 7
    Last Post: 06-20-2010, 04:33 PM
  2. Need help with scanner.
    By mainy in forum New To Java
    Replies: 3
    Last Post: 07-28-2009, 02:11 PM
  3. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 03:37 PM
  4. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 01:41 PM
  5. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •