While Java is generally used to create applets,
servlets, and general-purpose applications, you may occasionally need to create
applications that interactively communicate with a user at a command-line
prompt, such as a Unix or DOS prompt. In these cases you'll see a prompt that
looks something like this:
Enter your name: _
Unfortunately, this is one area where it's difficult
to use vanilla Java methods. If you're used to simple echo and read statements
in shell scripts, you're in for a bit of shock. I suspect the creators of Java
didn't expect to see their language used for this purpose, or they just assumed
developers would create their own classes to simplify this process.
we'll present
a technique we use when creating Java programs that require interactive
command-line input. If you like this basic technique, and would like to see
expanded coverage of how to read numeric values, or would like to see a custom
class for handling command-line input, just leave a comment below, and we'll
provide a follow-up as quickly as we can.
Reading
a string the user enters at a command-line prompt
The basic technique of reading a String provided by
a user at a command-line is fairly simple, but more lengthy than you'd expect.
It involves the use of the System.in object, along with the InputStreamReader
and BufferedReader classes. (See my Java BufferedReader examples for more
information on those classes.)
The code shows how you can prompt the user to enter
a String value, such as their name, and then read that value:
import java.io.*;
public class ReadString {
public static
void main (String[] args) {
// prompt the user to enter their name
System.out.print("Enter your name: ");
// open up standard input
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String
userName = null;
// read the username from the command-line; need
to use try/catch with the
// readLine() method
try {
userName = br.readLine();
} catch
(IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
System.out.println("Thanks for the name, " + userName);
}
} // end of
ReadString class
1The ReadString.java program shows how you can
prompt the user to enter their name, and then read the name into the userName
variable.
1 demonstrates how you can print a prompt to the
user using the System.out.print() method. Notice that we use the print() method
instead of println(). Using the print() method lets us keep the cursor on the
same line of output as our printed text. This makes it look like a real prompt
(instead of having the user's response appear on the line below our prompt).
Next, we read the user's input by passing the
System.in object to the InputStreamReader and then into the BufferedReader The
Java BufferedReader class gives us the readLine() method, and applies buffering
to the input character input stream. Notice that the readLine() method can
thrown an IOException error, so we have to enclose the statement in a try/catch
statement.
No comments:
Post a Comment