Re: java.util.Scanner and EOF/EOT issues



In article
<aede0094-c11d-4d51-906a-24a9e6af0422@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
sasuke <database666@xxxxxxxxx> wrote:

Eric Sosman wrote:

The infinite loop isn't in Scanner, but in your own code.
Your startTest() method contains a `while(true)' loop that has
nothing to terminate it: when the Scanner's hasNext() method
returns false, the loop just keeps on iterating. When you
type ^Z the Scanner gets end-of-file and reports it, but your
code just keeps on checking hasNext(). When you type ^C a
similar thing happens, but the JVM is in the process of shutting
down and will eventually exit and take your loop with it.

Try changing your code so that it does something different
when hasNext() returns false instead of true.

Indeed, thanks a lot for the reply. I was under the assumption that no
matter what is thrown at the STDIN, it will be treated as a token i.e.
pressing CTRL + Z would generate a true for hasNext() and invoking next
() would skip that but it seems I was wrong.

Another problem I stumbled across when using a Scanner. It seems as
though pressing multiple returns [ENTER] has no effect when using a
Scanner for accepting user input via STDIN but when using
BufferedReader, a single empty return without entering anything is
equivalent to a blank string being entered. My guess is that since the
Scanner is based on the concept of delimiters, pressing multiple
returns has the effect of hasNext() returning false until something
other than a return is entered by the user.
[...]

I see "The default whitespace delimiter used by a scanner is as
recognized by Character.isWhitespace," which includes line delimiters.
You might try to specify the DOTALL flag in your pattern:

<http://java.sun.com/javase/6/docs/api/java/util/Scanner.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
.



Relevant Pages

  • Re: java.util.Scanner and EOF/EOT issues
    ... Your startTestmethod contains a `while' loop that has ... code just keeps on checking hasNext(). ... Scanner for accepting user input via STDIN but when using ... recognized by Character.isWhitespace," which includes line delimiters. ...
    (comp.lang.java.programmer)
  • Re: java.util.Scanner and EOF/EOT issues
    ... The infinite loop isn't in Scanner, ... code just keeps on checking hasNext(). ... public void startBufTest() { ...
    (comp.lang.java.programmer)
  • Re: Constructor Help Needed
    ... import java.util.Scanner;//program uses scanner ... {// Start Rate Method ... {// Start hourly rate loop until positive number is entered ... // Returns the paycheck amount based on doubles, ...
    (comp.lang.java.programmer)
  • Re: OO Style with Ada Containers
    ... cursors aren't tagged because an operation can only be primitive for a ... Also included is a simple scanner ... while not End_Of_File loop ... (Word: String; ...
    (comp.lang.ada)
  • RE: Thread Events
    ... I've tried putting a loop in my thread. ... I then setscanstop=true in the interrupt handler ... > with built in scanner. ... > the scanner to notify my main thread when it successfully reads a barcode. ...
    (microsoft.public.pocketpc.developer)