The Artima Developer Community
Sponsored Link

Java Answers Forum
How To Get User Input from Command Prompt

2 replies on 1 page. Most recent reply: Feb 14, 2004 8:09 PM by leonglkw

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

How To Get User Input from Command Prompt Posted: Feb 14, 2004 10:19 AM
Reply to this message Reply
Advertisement
I am trying to write a program to accept user input.
This program will be run in Command Prompt.

My expected output is something as below :

Pls select option : (User will input S for Sales,
D for Daily Order,
C for Collection,
E for Exit)
Once the use input his/her input, If/Else will be used to verify the option chosen.
If the option chose are not one of the S, D, C or E,
the program will request the user input again.

I need to know how to write the code.
The help that all of you offered is most appreciated.
Thanks.


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: How To Get User Input from Command Prompt Posted: Feb 14, 2004 3:04 PM
Reply to this message Reply
To get a string value from the user after displaying a message you could use a simple method like the following.

I leave it to you to add it to your program or use it as you would like.

    /**  Displays the input message on the console to 
     *   prompt the user for input. Returns a line of 
     *   text entered from the console.
     */
    private String getInput(String message){
        String userInput = "";
        try{
            System.out.println(message);
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            userInput = br.readLine();
        }catch(IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());
        }
        return userInput;
    }
 

leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

Re: How To Get User Input from Command Prompt Posted: Feb 14, 2004 8:09 PM
Reply to this message Reply
Thanks for the help....

I need some more help...

After i have received the input from user, i need to write some code to determine the selection of the user...

For example:

Option : S

After the character is input, i will use if/else to see which option the user choose. By the way, every option will perform certain function....such as select S will perform Sales, select D will perform Dailly Order, and C will perform a dosplay of the current sales info and E will exit the program...

The most important thing is that i do not know how to write the if/else...

Flat View: This topic has 2 replies on 1 page
Topic: Create a directory on the server Previous Topic   Next Topic Topic: japplet vs jframe

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use