Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Reinventing the Art of Creating Command-line Java Apps-3 : Page 3


advertisement
Write the Code
"System.out.println()" was probably the first method call you ever learned in Java (I mean, what easier way could you fulfill your need to produce the infamous "Hello World" output?). You use this call all the time, but do you understand its etymology? Here's a quick lesson. The System class is composed entirely of public static methods. This means that you never instantiate objects of type System. Instead, System class methods are invoked directly by qualifying them with the class name and a period "( System.method() )". So how does "out" fit into all of this? "out" is a static member of the System class. It refers to the standard output stream, which is an unbuffered Printstream sent to the console by default.

Reading input from the command line is the exact opposite of sending output to the console. Fortunately, Java has created a very consistently designed framework. The counterpart to the static class member out (which refers to the standard output stream), is the static class member in (which refers to the standard input stream). The following code will read one character from the keyboard:

char c = System.in.read();



While this may be useful for you in a few cases, it is not a very flexible solution because the standard input stream can only read one character at a time. To improve on this, you can wrap a BufferedReader object around the standard input stream like this:

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

Now that you have a reference to a BufferedReader object, you are free to retrieve entire strings from the command line, using the readLine() method defined in the BufferedReader class:

String input = in.readLine();

Since you are likely to need several pieces of input from your user for a given application, it will be more efficient to put this code inside a method. Here's an example of a method which takes a single string as a parameter and displays that string as the prompt to the user for input:

private static String getInputString ( String prompt ) throws IOException{ System.out.print( prompt ); return in.readLine(); } //end getInputString()

There are many ways that you could expand upon this framework. You create several methods that returned different primitive types (char, double, int, etc.), or even create an entire class of static methods that you could reuse in all of your applications by using a simple import statement.

Pull everything together
Now that the input retrieval method(s) are written, it is time to pull everything together into a simple example. Since we're dealing with I/O, we'll need to import the java.io package into our class:

import java.io.*;

Then we'll need to declare the BufferedReader object outside of the main() method, so that it can be used in the getInputString() method.

Buffered Reader in;

Then wrap everything in a try/catch block in the main() method. The two uses of the BufferedReader object (invocation and in.readLine() ) both have the potential to throw an IOException. Here's what the main() method looks like:

public static void main( String[] args ) { try{ in = BufferedReader( new InputStreamReader( System.in ) ); String name = getInputString( "What is your name? " ); System.out.println( "Your name is " + name ); } catch ( IOException ioe ) ioe.printStackTrace(); } //end try{} } //end main()

And that's it! You've taken input from the user and displayed it on the screen. Not very impressive, you say? Try creating a calculator, random number guessing game, or a keyword search utility.

Sample Applications
On this page are links to the source code for two interactive command line applications.

AreaCalc.java is a simple class that calculates the area of a circle, rectangle, or triangle, based on values specified by the user.

ServletClient.java is a utility that can be used to test the output produced by Java servlets. It issues a GET request against the specified URL based on the specified host and port.



DevX Java Pro Kyle Gabhart is senior software engineer for Brainbench who has developed and presented Java courseware and is an "expert" on Askme.com. He currently is collaborating with WROX press on a new Java and XML book.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date