The user has entered characters from the keyboard which are then transformed into primitive numeric types.
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.io.*; class AddTwo { public static void main ( String[] args ) throws IOException { int numberA, numberB; String line; BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); System.out.println("Enter first number:"); line = stdin.readLine(); numberA = Integer.parseInt( line ); System.out.println("Enter second number:"); line = stdin.readLine(); numberB = Integer.parseInt( line ); 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 C:\users\default\JavaLessons>