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

Answer:

No.


Numeric Input

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) );
  }
}

The above program does numeric input from the keyboard, and can be used with input redirection. Here it adds up two integers entered by the user:

C:\temp>java AddTwo
Enter first number: 12
Enter second number: 7
Sum: 19

The program reads character groups like "12" entered by the user and converts them into the int data type using:

scan.nextInt()

This method reads the input stream only far enough to gather characters for one integer. It scans over spaces until it finds characters that can be used for an integer. If it hits characters that won't work for an int, it throws an Exception.

It stops in the middle of a line after it has finished reading one integer. The next time it is called it continues from where it left off.


QUESTION 8:

C:\temp>java AddTwo
Enter first number: 4   13
Enter second number:   Sum: 17

What happened here ?


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