| Developer's Daily | Java Education |
| front page | java | perl | unix | DevDirectory |
Introduction
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, such as a Unix or DOS prompt. In these cases you normally see a prompt like this:
While this is often the domain of scripting languages, you may want to use Java for this purpose to take advantage of how easy it is to accomplish most tasks with Java - especially networking and database access.
Unfortunately, this is one area where it's difficult to use vanilla Java methods. If you're used to simple echo and read statements, 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.
In this article 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 write us by using the link at the bottom of this article, 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. The code in Listing 1
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
// open up standard input
String userName = null; // read the username from the
command-line; need to use try/catch with the
System.out.println("Thanks for the name, " + userName); } } // end of ReadString class
|
| Listing 1: | The ReadString.java program shows how you can prompt the user to enter their name, and then read the name into the userName variable. |
Listing 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 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.
Final thoughts and source code
As you can see from Listing 1, reading user input from the command-line is a little cumbersome with Java. We're also prompting the user for only one input value. You can imagine what the code looks like when you're prompting the user for multiple input values. It gets even worse if you want to read integers or floating-point values from the user, because those values require additional conversion.
If you're interested in a follow-up article demonstrating how to read numeric values, or you're interested in seeing us develop a class to make command-line input much easier, write us at purejava@DevDaily.com. We'll post a new article as soon as we can develop it (We'll gladly do this if there is enough interest. Because this is supposedly not a "mainstream" use of the Java language, we decided to only show the basic technique here. But, then again, don't let us tell you what's mainstream and what's not!).
If you'd like to download the ReadString.java code shown in Listing 1, just click here, and the Java code will be displayed in your browser. Then just use the "File | Save As" option of your browser to save the source code to your system.
Copyright 1998-2008 DevDaily Interactive, Inc.
All Rights Reserved.