JAVA : Reading Input from the Console

Reading Input from the Console


Reading input from the console enables the program to accept input from the user.
Console input is not directly supported in Java, but you can use the Scanner class to create an object to read input from System.in, as follows:
Scanner input = new Scanner(System.in);
Above statement creates a Scanner object and assigns its reference to the variable input. An object may invoke its methods. To invoke a method on an object (variable input) is to ask the object to perform a task. You can invoke the nextDouble() method to read a double value as follows:
double radius = input.nextDouble();

Program : ComputeAreaWithConsoleInput.java 


The specific import specifies a single class in the import statement. The wildcard import imports all the classes in a package by using the asterisk as the wildcard. There is no performance difference between a specific import and a wildcard import declaration.

Program : ComputeAverage.java


Comments