In the previous example, we took command line parameter as input. In this example, the program would prompt users for input and then it would process their input. The following programs simply divides two numbers.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Division {
public static void main(String[] args) {
int divident = 0;
int divisor = 0;
double quotient = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Divident (integer): ");
try {
divident = Integer.valueOf(br.readLine());
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("Divisor (integer): ");
try {
divisor = Integer.valueOf(br.readLine());
} catch (IOException ex) {
ex.printStackTrace();
}
quotient = divident / divisor;
System.out.println("Quotient: " + quotient);
}
}
Explanation
In the 11th line, input is reader by InputStreamReader and stored in BufferedReader. Both values are casted to integers.