HelloName.javaFrom WikiJavabuy 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.
the articleHelloName.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
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 |
