Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Eclipse 1.3 and Scanner hasNext()

 
Antonio Vieira
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

The following code doesn't return when scan.hasNext() is called:

public static void main(String[] args) {
System.out.println("starting");
Scanner scan = new Scanner(System.in);
while(scan.hasNext()) {
System.out.println(scan.next());
}
System.out.println("done");
}

The following code works:

public static void main(String[] args) {
System.out.println("starting");
Scanner scan = new Scanner("TEST");
while(scan.hasNext()) {
System.out.println(scan.next());
}
System.out.println("done");
}

The code segments are being compiled and run in Eclipse 1.3 with jdk1.5. The first program is run with the following program arguments:

hey oh lets go

Any help is appreciated.

Thanks
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The System.in variable refers to what is called "standard input" or "stdin" for short. If you run your first program from the command line, it will read input that you type from the keyboard. My guess is that either Eclipse has a separate window that it uses as stdin or it doesn't redirect the console at all. In the first case, you need to find the console window; in the seconde, you need to use a different input method or run it from the command line.

Note that this is separate from the command-line arguments that you specified. Those are available through the String[] parameter that is passed to main().

HTH

Layne
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe it should be Eclipse 3.1 ( not 1.3 as mentioned).

You may type the input in the console window. To indicate the end of your input you may press ctrl+z and the while loop will complete and print "done"
[ January 20, 2006: Message edited by: karthi keyan ]
 
Antonio Vieira
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.

I must correct myself, the Eclipse verion I'm using is indeed 3.1.1.

Problem solved. Here are the details:

1. I was under the impression that the Scanner could be used to read program arguments, that is arguments provided in String [] args. This is incorrect, I now understand that the Scanner object is used, in my particular case, to read standard input as Layne pointed out.
2. The program was blocking waiting for the standard input to be provided.
3. Once provided the program would execute the sequences in the while loop.
4. From the command line I acheived this using the following "java class_name < input file". Alternatively, I would not provide the input file, the program would wait for input, I would enter it directly into the terminal window, and the output would be printed.
5. From Eclipse I opened the console view and was able to enter input directly into the window.

I appreciate your help and I hope this will help others experiencing the same problem.

Thanks
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Antonio Vieira:

4. From the command line I acheived this using the following "java class_name < input file". Alternatively, I would not provide the input file, the program would wait for input, I would enter it directly into the terminal window, and the output would be printed.

Notice that this has nothing to do with Java. By using this command-line syntax, the operating system redirects stdin from the input file. From your program's point of view, it is still using stdin to get its input.

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Antonio Vieira:
Thanks for the replies.

I must correct myself, the Eclipse verion I'm using is indeed 3.1.1.

Problem solved. Here are the details:

1. I was under the impression that the Scanner could be used to read program arguments, that is arguments provided in String [] args. This is incorrect, I now understand that the Scanner object is used, in my particular case, to read standard input as Layne pointed out.

You can, in fact, use Scanner to read from the program arguments by using a variation on your second program. (Can you figure out how? As a hint, it might help if you write ANOTHER program that simply prints out the command-line arguments.) Notice that in the first program, Scanner reads from stdin because you TOLD it to with

Here System.in refers to stdin. There is no particular connection between Scanner and stdin other than what you have explicitly stated here in your code by using System.in. That is why I didn't even mention Scanner in my response to your question.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic