Core Java Tutorials - Reading Input from the keyboard in Java, How to read input from keyword in Java, Scanner Class in Java

Reading Input from the keyboard in Java, How to read input from keyword in Java, Scanner Class in Java

Java uses System.out to refer to the standard output device and to the standard input device. These are the InputStream and OutputStream class's methods. By default the output device is the display monitor, and the input device is the keyboard. To perform console output, you simply use the println() method to display a primitive value or a string to the console. Console input is not directly supported in Java but,
There are several method to reading input from the console in java some of them are as follwos:-

  • Console Class
  • DataInputStream Class 
  • Scanner Class

I am using the Scanner class to create an object to read input from, as follows:

Scanner input = new Scanner(;

The above statement creates a input object of Scanner type with the help of new Scanner( Here input is a variable whose type is Scanner. The whole line Scanner input = new Scanner( creates a Scanner object and assigns its reference to the variable input.This object able to invoke any method of Scanner Class.
Here is The list of some Scanner Class Methods.

Methods for Scanner Objects
nextByte()reads an integer of the byte type.
nextShort()reads an integer of the short type.
nextInt()reads an integer of the int type.
nextLong()reads an integer of the long type.
nextFloat()reads a number of the float type.
nextDouble()reads a number of the double type.
next()reads a string that ends before a whitespace character.
nextLine()reads a line of text (i.e., a string ending with the Enter key pressed).

public class ExecuteCircleArea
public static void main(String st[]) {
// Create a Scanner Class object
Scanner in = new Scanner(;
// Prompt a message to the user to enter a radius 

System.out.print("Enter a number for radius: ");
double r =in.nextDouble();
// Compute area 
double area = r * r * 3.14159; 
// Display result  of area
System.out.println("The area for the circle of radius " + 
r + " is " + area); 

{ 0 comments... read them below or add one }

Post a Comment