wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Having problem with Scanner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having problem with Scanner" Watch "Having problem with Scanner" New topic
Author

Having problem with Scanner

Programmer Paul
Greenhorn

Joined: Aug 02, 2005
Posts: 1
Hi all, I am experencing some strange problems with the new Scanner class. I am running Java 1.5, update 4 on Windows XP. I have the code below, done using JCreator Pro:



The program works fine, but if I attempt to redirect input from a file, such as:

java test < commands.txt

it will crash with a NoSuchElementException.

Any idea what I am doing wrong?
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
The problem has to do with your attempt to open an input stream using System.in, followed by another attempt to open an input stream using System.in without having closed the first stream. Here's a version of your program that doesn't have that problem:



SCJA 1.0 (98%), SCJP 1.4 (98%)
 
jQuery in Action, 2nd edition
 
subject: Having problem with Scanner
 
Similar Threads
input from console
Help needed with Scanner
Loop through the list of objects to get attribute.
What is wrong in this code ?
Question about restart