Thursday, 24th June 2010
The new frontier for learning Java

HelloName.java

From WikiJava

Jump to: navigation, search
The author suggests:

buy this book

HelloName.java is a standalone program that asks for the name on the standard output (screen), reads the string given in standard input (keyboard), and writes a kind greeting to the user.

Contents

the article

HelloName.java is the simplest estension of HelloWorld.java.

It uses java.io.InputStreamReader to read characters from the System.in and inserts them into a java.io.BufferedReader so all the inserted character are then available as a java.lang.String for easier processing.

The InputStreamReader gets initialized with a InputStream which in this case is the standard input System.in. It is then wrapped into the BufferedReader which reads and inserts into a buffer the inserted characters.

the bufferedReader.readLine(); is then used to retrieve the first line (the characters pressed before the return).

HelloName.java

Image:250px-Subversion.png
You can download the complete code of this article from the Subversion repository at this link

Using the username:readonly and password: readonly

See the using the SVN repository instructions page for more help about this.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
 
/**
 * Hello Name
 * 
 * @author Giulio
 * @date 15/12/2007
 */
public class HelloName {
 
    /**
     * Prints on the default output a greeting to a person.
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
	System.out.println("What's your name? ");
 
	// the BufferedReader is able to store some data for easier retrieval
	BufferedReader bufferedReader = new BufferedReader(
		new InputStreamReader(System.in));
	String name = bufferedReader.readLine();
 
	System.out.println("Hello " + name +"!!!");
    }
 
}

See Also

helloworld.java

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Title (required):

Website:

Comment: