• Post Reply Bookmark Topic Watch Topic
  • New Topic

Command Prompt Input  RSS feed

 
colin shuker
Ranch Hand
Posts: 750
  • 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
  • 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
  • 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
  • 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.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!