go to previous page   go to home page   go to next page        


The user has entered characters from the keyboard which are then transformed into primitive numeric types.

Numeric Input

The same method is used for numeric input using a redirected input file. The program is written to do numeric input from the keyboard, then connected to a data file by redirection. Here is a program that adds up two integers entered from the keyboard:

import java.util.Scanner;
class AddTwo
  public static void main ( String[] args ) 
    int numberA, numberB;
    Scanner scan = new Scanner( System.in );

    System.out.print("Enter first number: ");
    numberA = scan.nextInt();

    System.out.print("Enter second number: ");
    numberB = scan.nextInt();

    System.out.println( "Sum: " + (numberA + numberB) );

Here is its normal operation:

C:/users/default/JavaLessons>java AddTwo
Enter first number: 12
Enter second number: 7
Sum: 19


Why are there parentheses around (numberA + numberB) ?