File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Having problem with Scanner

Programmer Paul

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, followed by another attempt to open an input stream using 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%)
I agree. Here's the link:
subject: Having problem with Scanner
It's not a secret anymore!