Command line arguments

I need help setting up a samll command line app that functions as a bank. I want to echo a small menu to the user and give the user choices on withdrawls, deposits etc. I understand how to accept parameters with the initial java command but don't know how to set it up to wait for additional input. Any advice is greatly appreciated.

thanks,

Dan

[365 byte] By [grin1dana] at [2007-10-3 2:40:16]
# 1
Presumably this is an assignment since normally you would use a GUI for what you described.Look at System.out for output (printing the menu) and System.in for input.
jschella at 2007-7-14 19:38:37 > top of Java-index,Java Essentials,New To Java...
# 2
Thank you.
grin1dana at 2007-7-14 19:38:37 > top of Java-index,Java Essentials,New To Java...
# 3
You may also want to take a look at java.util.Scanner or java.io.BufferedReader with java.io.InputStreamReader.
paulcwa at 2007-7-14 19:38:37 > top of Java-index,Java Essentials,New To Java...
# 4
Would you mind giving me a simple example with just a few lines of code? Many thanks.
grin1dana at 2007-7-14 19:38:37 > top of Java-index,Java Essentials,New To Java...
# 5

this is about the simplest form of user input. i pulled it out of my samples library, but it was probably originally from www.javaalmanac.com (as is most of my stuff :p)

public class UserInput {

public static void main (String[] args) {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

System.out.println ("Enter some text");

String input = "";

try {

input = reader.readLine();

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println("you typed: " + input);

}

}

TimSparqa at 2007-7-14 19:38:37 > top of Java-index,Java Essentials,New To Java...
# 6

The idiom with BufferedReader (for multiple lines of input) is:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String line;

while((line = in.readLine()) != null) {

// line now contains one line of input

// when the user stops giving input, such as at end of file or when a user

// typing input enters control-d on unix systems, readLine will return null

// and this loop will end

}

Scanner apparently is now (as of JDK 1.5) the preferred way of doing this kind of thing, but I'm not very familiar with it.

paulcwa at 2007-7-14 19:38:37 > top of Java-index,Java Essentials,New To Java...