Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get user input?

 
M.L. Bowe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program that includes a menu and will do something different for each menu choice, but I am stuck on how to get the menu selection from the user...
Thanks in advance!
 
Jean-Christian Imbeault
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll have to post a more detailed question in order to get good replies.

What kind of program? GUI, command line?

I'm assuming it's a GUI program. Have you written the GUI yet? Have you implemented the MenuBar?

There are some good tutorials out there on how to implement MenuBars in a Java GUI.
 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bowe,
If you are using a menu which has menu items (menu = file, menu item= new) you can add the action listener to the menu items and in the actionPerformed() you can check which menu item was clicked and do what you want to do. Hope this helps.
 
M.L. Bowe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is actually an application that has a menu (no GUI). That is my big problem - I have the application so that it prints out the menu, but now I need the user selection to tell the program what to do from there.
Thanks.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whht you will want to do is read from System.in in order to do this. It is an InputStream, so check out the java doc for that.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing we all found disappointing the first time is that you can't get a single keystroke menu choice from the user. You have to make the user press Enter to get anything. Reading from the console is just annoyingly fussy enough that you might want to make a little utility class to hold some reusable code. By way of example, think about how you'd make this work:

If you're in JDK 5 see if you can use Scanner instead of BufferedReader. And let me know the answer!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic