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]

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);
}
}
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.