Java Data Type How to - Read Integers from console and calculate








Question

We would like to know how to read Integers from console and calculate.

Answer

import java.util.Scanner;
// w w  w . jav  a2s  .co m
public class MainClass
{
   public static void main( String args[] )
   {
      Scanner input = new Scanner( System.in );

      int x;
      int y;
      int z;
      int result;

      System.out.print( "Enter first integer: " );
      x = input.nextInt();

      System.out.print( "Enter second integer: " );
      y = input.nextInt();
      
      System.out.print( "Enter third integer: " );
      z = input.nextInt();

      result = x * y * z;

      System.out.printf( "Product is %d\n", result );

   }
}

The code above generates the following result.