.

User Input from Keyboard

Accepting keyboard input in Java is done using a Scanner object.

Consider the following statement

Scanner console = new Scanner(System.in)

This statement declares a reference variable named console. The Scanner object is associated with standard input device (System.in).

To get input from keyboard, you can call methods of Scanner class. For example in following statment nextInt() method of Scanner takes an integer and returns to variable x :

int x = console.nextInt();

Example :

import java.util.Scanner;    // Needed for Scanner class

/**
 *  This program demonstrates keyboard input.
 */
public class RectangleArea
{
   public static void main(String[] args)
   {
      int length;    // To hold rectangle's length.
      int width;     // To hold rectangle's width.
      int area;      // To hold rectangle's area

      // Create a Scanner object to read input.
      Scanner console = new Scanner(System.in);

      // Get length from the user.
      System.out.print("Enter length ");
      length = console.nextInt();

      // Get width from the user.
      System.out.print("Enter width ");
      width = console.nextInt();

      // Calculate area.
      area = length * width;

      // Display area.
      System.out.println("The area of rectangle is " + area);
   }
}

Output :

Enter length 5
Enter width 8
The area of rectangle is 40

 

Some other useful methods of Scanner class

Method Returns
int nextInt() Returns the next token as an int.
float nextFloat() Returns the next token as a float.
double nextDouble() Returns the next token as a long.
String next() Finds and returns the next complete token as a string ended by a blank.
String nextLine() Returns the rest of the current line, excluding any line separator at the end.

Example :

import java.util.Scanner;

/**
 * This program demonstrates various Scanner methods.
 */

public class ReadEmployee
{
    public static void main(String[] args)
    {
        String name; // To hold the employee's name
        int age; // To hold the employee's age
        char gender; // To hold the employee's gender
        double salary; // To hold the employee's salary

        // Create a Scanner object to read input.
        Scanner console = new Scanner(System.in);

        // Get the employee's name
        System.out.print("Enter name: ");
        name = console.nextLine();

        // Get the employee's age
        System.out.print("Enter age: ");
        age = console.nextInt();

        // Get the employee's gender
        System.out.print("Enter gender: ");
        gender = console.next().charAt(0);

        // Get the employee's salary
        System.out.print("Enter salary: ");
        salary = console.nextDouble();

        // Display the information
        System.out.println("Name: " + name + " Age: " + age + " Gender: "
                + gender + " Salary: " + salary);

    }
}

Output :

Enter name: Alex Joseph
Enter age: 24
Enter gender: M
Enter salary: 8000
Name: Alex Joseph Age: 24 Gender: M Salary: 8000.0