No.
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) ); } }
This example program is written to do 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 the characters like "12" that the user enters
and converts them into the int
data type using:
scan.nextInt()
This method reads the input stream only far enough to gather characters for the next integer. It will stop in the middle of a line after it has finished reading one integer. The next time it is called it will continue from where it left off.
Why are there parentheses around (numberA + numberB)
?