Program 20.4 A Personalized Hello

Program 20.4 is yet another variation on Hello World. This one asks the user for their name and then prints a personalized greeting.


import java.io.DataInputStream;
import java.io.IOException;
 
class PersonalHello {
 
  public static void main (String args[]) {
    
    System.out.println("What is your name?");
    try {
      DataInputStream myInput = new DataInputStream(System.in);
      String name = myInput.readLine();
      System.out.print("Hello ");
      System.out.println(name);
    }
    catch (IOException e) {
      System.out.println("I'm Sorry.  I didn't catch your name.");
    }
      
  }
    
}

Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home