How many ways can we read data from the keyboard in Java?


The java.io package provides various classes to read write data from various sources and destinations.

You can read data from user (keyboard) using various classes such as, Scanner, BufferedReader, InputStreamReader, Console etc.

Using Scanner class

From Java 1.5 Scanner class was introduced. This class accepts a File, InputStream, Path and, String objects, reads all the primitive data types and Strings (from the given source) token by token using regular expressions. By default, whitespace is considered as the delimiter (to break the data into tokens).

To read data from keyboard you need to use standard input as source (System.in). For each datatype a nextXXX() is provided namely, nextInt(), nextShort(), nextFloat(), nextLong(), nextBigDecimal(), nextBigInteger(), nextLong(), nextShort(), nextDouble(), nextByte(), nextFloat(), next().

Example

Following Java program reads data from user using the Scanner class.

import java.util.Scanner;
class Student{
   String name;
   int age;
   float percent;
   boolean isLocal;
   char grade;
   Student(String name, int age, float percent, boolean isLocal, char grade){
      this.name = name;
      this.age = age;
      this.percent = percent;
      this.isLocal = isLocal;
      this.grade = grade;
   }
   public void displayDetails(){
      System.out.println("Details..............");
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
      System.out.println("Percent: "+this.percent);
      if(this.isLocal) {
         System.out.println("Nationality: Indian");
      }else {
         System.out.println("Nationality: Foreigner");
      }
      System.out.println("Grade: "+this.grade);
   }
}
public class ReadData {
   public static void main(String args[]) {
      Scanner sc =new Scanner(System.in);
      System.out.println("Enter your name: ");
      String name = sc.next();
      System.out.println("Enter your age: ");
      int age = sc.nextInt();
      System.out.println("Enter your percent: ");
      float percent = sc.nextFloat();
      System.out.println("Are you local (enter true or false): ");
      boolean isLocal = sc.nextBoolean();
      System.out.println("Enter your grade(enter A, or, B or, C or, D): ");
      char grade = sc.next().toCharArray()[0];
      Student std = new Student(name, age, percent, isLocal, grade);
      std.displayDetails();
   }
}

Output

Enter your name:
Krishna
Enter your age:
25
Enter your percent:
86
Are you local (enter true or false):
true
Enter your grade(enter A, or, B or, C or, D):
A
Details..............
Name: Krishna
Age: 25
Percent: 86.0
Nationality: Indian
Grade: A

Using BufferedReader

The BufferedReader class of Java is used to read stream of characters from the specified source (character-input stream). The constructor of this class accepts an InputStream object as a parameter, you can to pass an InputStreamReader.

Example

Following Java program reads data from user using the BufferedReader class.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Student{
   String name;
   int age;
   float percent;
   boolean isLocal;
   char grade;
   Student(String name, int age, float percent, boolean isLocal, char grade){
      this.name = name;
      this.age = age;
      this.percent = percent;
      this.isLocal = isLocal;
      this.grade = grade;
   }
   public void displayDetails(){
      System.out.println("Details..............");
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
      System.out.println("Percent: "+this.percent);
      if(this.isLocal) {
         System.out.println("Nationality: Indian");
      }else {
         System.out.println("Nationality: Foreigner");
      }
      System.out.println("Grade: "+this.grade);
   }
}
public class ReadData {
   public static void main(String args[]) throws IOException {
      BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter your name: ");
      String name = reader.readLine();
      System.out.println("Enter your age: ");
      int age = Integer.parseInt(reader.readLine());
      System.out.println("Enter your percent: ");
      float percent = Float.parseFloat(reader.readLine());
      System.out.println("Are you local (enter true or false): ");
      boolean isLocal = Boolean.parseBoolean(reader.readLine());
      System.out.println("Enter your grade(enter A, or, B or, C or, D): ");
      char grade = (char) reader.read();
      Student std = new Student(name, age, percent, isLocal, grade);
      std.displayDetails();
   }
}

Output

Enter your name:
Krishna
Enter your age:
25
Enter your percent:
86
Are you local (enter true or false):
true
Enter your grade(enter A, or, B or, C or, D):
A
Details..............
Name: Krishna
Age: 25
Percent: 86.0
Nationality: Indian
Grade: A

Using Console class

This class is used to write/read data from the console (keyboard/screen) devices. It provides a readLine() method which reads a line from the key-board. You can get an object of the Console class using the console() method.

Note − If you try to execute this program in a non-interactive environment like IDE it doesn’t work.

Example

Following Java program reads data from user using the Console class.

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
class Student{
   String name;
   int age;
   float percent;
   boolean isLocal;
   char grade;
   Student(String name, int age, float percent, boolean isLocal, char grade){
      this.name = name;
      this.age = age;
      this.percent = percent;
      this.isLocal = isLocal;
      this.grade = grade;
   }
   public void displayDetails(){
      System.out.println("Details..............");
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
      System.out.println("Percent: "+this.percent);
      if(this.isLocal) {
         System.out.println("Nationality: Indian");
      }else {
         System.out.println("Nationality: Foreigner");
      }
      System.out.println("Grade: "+this.grade);
   }
}
public class ReadData {
   public static void main(String args[]) throws IOException {
      Console console = System.console();
      if (console == null) {
         System.out.println("Console is not supported");
         System.exit(1);
      }
      System.out.println("Enter your name: ");
      String name = console.readLine();
      System.out.println("Enter your age: ");
      int age = Integer.parseInt(console.readLine());
      System.out.println("Enter your percent: ");
      float percent = Float.parseFloat(console.readLine());
      System.out.println("Are you local (enter true or false): ");
      boolean isLocal = Boolean.parseBoolean(console.readLine());
      System.out.println("Enter your grade(enter A, or, B or, C or, D): ");
      char grade = console.readLine().toCharArray()[0];
      Student std = new Student(name, age, percent, isLocal, grade);
      std.displayDetails();
   }
}

Output

Enter your name:
Krishna
Enter your age:
26
Enter your percent:
86
Are you local (enter true or false):
true
Enter your grade(enter A, or, B or, C or, D):
A
Details..............
Name: Krishna
Age: 26
Percent: 86.0
Nationality: Indian
Grade: A

Updated on: 01-Aug-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements