Chapter 2: User Input and Arithmetic

Unfortunately, getting information from the user is not as easy in Java as it is in other languages. Most of the time, user input is obtained from windows forms, or web forms. Since we aren't at that level yet, we are going to have to get user input from the console.

      CODE:
        1: /* Title:  UserInput1.java   
        2:    Author: Jason Snyder    */
        3:
        4: import java.util.Scanner;
        5: 
        6: public class UserInput1
        7: {
        8:    //Main Method
        9:    public static void main( String[] args )
       10:    {
       11:       Scanner stdin = new Scanner( System.in );
       12:
       13:       String x;
       14:
       15:       System.out.print( "Please enter your name: " );
       16:
       17:       x = stdin.nextLine();
       18:
       19:       System.out.printf( "Hello %s, how are you doing today?", x );
       20:     }//End Main Method
       21: }//End Class UserInput1
   
      OUTPUT:
         Please enter your name: Jason
         Hello Jason, how are you doing today?
   

Ok, so let's go through this program focusing what's new.

The first new thing appears on line 4:

        4: import java.util.*;
   

This is called an import declaration, it tells Java to locate classes and include them in the program. Here, we tell Java to include the class Scanner in the Java.util library. This actually demonstrates a very powerfull tool that Java gives you. The power to reuse others code. Why should you have to reinvent the wheel everytime you need to solve a problem? Java allows you to, through it's vast libraries, reuse solutions that someone else has solved before. Here, we use Scanner, a solution to the user input problem.

The next new item comes in line 11:

       11:       Scanner stdin = new Scanner( System.in );
   

This is called a variable declaration. This line delcares the name and type of a variable. A variable is a location in your computer memory that stores a value for future use in the program. In this line we decalare a variable of type Scanner and give it the name stdin.

      CODE:
        1: /* Title:  UserInput2.java   
        2:    Author: Jason Snyder    */
        3:
        4: import java.util.*;
        5:
        6: public class UserInput2
        7: {
        8:    //Main Method
        9:    public static void main( String[] args )
       10:    {
       11:       Scanner stdin = new Scanner( System.in );
       12:      
       13:       int integer1;
       14:       int integer2;
       15:       int sum;
       16:
       17:       System.out.print( "Please enter an integer: " );
       18:       integer1 = stdin.nextInt();
       19:
       20:       System.out.print( "Please enter another integer: " );
       21:       integer2 = stdin.nextInt();
       22:
       23:       sum = integer1 + integer2;
       24:       
       25:       System.out.printf( "%d + %d = %d", integer1, integer2, sum );
       26:    }//End Main Method
       27: }//End Class UserInput2
   
      OUTPUT:
         Please enter an integer: 12
	 Please enter another integer: 25
	 12 + 25 = 37