This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Command Prompt Input

 
colin shuker
Ranch Hand
Posts: 750
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
Linux Python VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
colin shuker
Ranch Hand
Posts: 750
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic