Re: java.util.Scanner and EOF/EOT issues
- From: "John B. Matthews" <nospam@xxxxxxxxxx>
- Date: Sun, 25 Jan 2009 12:30:02 -0500
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>
.
- Follow-Ups:
- Re: java.util.Scanner and EOF/EOT issues
- From: sasuke
- Re: java.util.Scanner and EOF/EOT issues
- References:
- java.util.Scanner and EOF/EOT issues
- From: sasuke
- Re: java.util.Scanner and EOF/EOT issues
- From: Eric Sosman
- Re: java.util.Scanner and EOF/EOT issues
- From: sasuke
- java.util.Scanner and EOF/EOT issues
- Prev by Date: CLICK AND GET MONEY
- Next by Date: Re: Convenience constructors and non-final setters
- Previous by thread: Re: java.util.Scanner and EOF/EOT issues
- Next by thread: Re: java.util.Scanner and EOF/EOT issues
- Index(es):
Relevant Pages
|