• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Command Prompt Input

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 97
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic