File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to get user input? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get user input?" Watch "How to get user input?" New topic

How to get user input?

M.L. Bowe

Joined: Dec 09, 2004
Posts: 7
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

Joined: Dec 08, 2004
Posts: 19
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

Joined: Feb 18, 2003
Posts: 524
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.

SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
M.L. Bowe

Joined: Dec 09, 2004
Posts: 7
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.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Whht you will want to do is read from in order to do this. It is an InputStream, so check out the java doc for that.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: How to get user input?
It's not a secret anymore!