Greet2.java

Download Greet2.java

 1: /* A simple program to display a friendly greeting.  This stand-alone
 2:  * non-GUI Java program demonstrates keyboard input the easy way, using the
 3:  * new Java5 "Scanner" class.  It shows checking for and reading in both
 4:  * a String and an int.  (Left as an exercise: test if user enters:
 5:  *   name, age
 6:  * and if so, remote the trailing comma from name.)
 7:  *
 8:  * Written by Wayne Pollock, Tampa, FL USA, 2005.
 9:  */
10: 
11: import java.util.Scanner;
12: 
13: class Greet2
14: {
15: 
16:    public static void main ( String [] args )
17:    {
18: 	  Scanner in = new Scanner( System.in );
19: 	  String name = "Anonymous";
20: 	  int age = Integer.MAX_VALUE;
21: 
22: 	  System.out.print( "Please enter your first name and age: " );
23: 
24:       if ( in.hasNext() )
25:          name = in.next();
26: 
27:       if ( in.hasNextInt() )
28:          age = in.nextInt();
29: 
30: 	  System.out.println( "\nHowdy " + name + "!" );
31: 
32: 	  if ( age == Integer.MAX_VALUE || age < 1 )
33: 	     System.out.println( "That's not a valid age!" );
34: 	  else if ( age < 6 )
35:          System.out.println( "How adorable!" );
36:       else if ( age < 10 )
37: 	     System.out.println( "My how you've grown!" );
38: 	  else if ( age < 13 )
39: 	     System.out.println( "You're all grown up!" );
40: 	  else if ( age < 21 )
41: 	     System.out.println( "Hey, dude!" );
42: 	  else if ( age < 40 )
43: 	     System.out.println( "Have a great day!" );
44: 	  else
45: 	     System.out.println( "My, how young you look!" );
46: 
47:    }  // End of main
48: 
49: }  // End of class Greet2