Numeric Input

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

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

QUESTION 8:

Why are there parentheses around (numberA + numberB) ?