Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having problem with Scanner

 
Programmer Paul
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic