GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Command Prompt Input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Command Prompt Input" Watch "Command Prompt Input" New topic
Author

Command Prompt Input

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I'm making a text based game in the command prompt.

What would be a simple way to respond to a press of enter button.

Thanks for any help, much appreciated.
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

You will probably use the System.in input stream so you will want to look at the following classes:
java.lang.System
java.io.InputStream
java.nio.InputStreamReader
java.io.BufferedReader

what I would do is wrap the System.in into a InputStreamReader, which I would then wrap in a BufferedReader eg

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

then you can use BufferedReader's readLine() method to see when the user presses enter. Probably not as simple as you are looking for, but I think that is about as simple as it gets with the standard API


www.mormon.org
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks, I tried:

Scanner in = new Scanner(System.in);
in.nextLine();

And this seems to work, I just donno if its the right way of doing things
Michael Keisu
Ranch Hand

Joined: Oct 08, 2009
Posts: 30
colin shuker wrote:Thanks, I tried:

Scanner in = new Scanner(System.in);
in.nextLine();

And this seems to work, I just donno if its the right way of doing things


It's the best way, imo.

And when you get started with swing, you wont care for it anyway.

 
GeeCON Prague 2014
 
subject: Command Prompt Input