المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Reading a string the user enters at a command-line prompt in Java



A7med Baraka
10-30-2008, 11:55 PM
Reading interactive command-line input


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:

Enter your name: _


While this is often the domain of scripting ********s, 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 ******** 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
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


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.

wa7edmenelnass
10-31-2008, 12:30 AM
Thanks alot
Nice Work ya Man !
Yours