How To Get Data From Keyboard In Java

Introduction

In this article we discuss how to get data from the keyboard in Java.

Some ways to get data from the keyboard

The following are some ways to get data from the keyboard:

  1. Console (I/O) class
  2. BufferedReader (and InputStreamReader) class
  3. Scanner class
  4. DataInputStream class

1. Console (I/O) class

The Console (I/O) class reads from the keyboard.

Object

The System class provides a static method named "console" that returns the unique instance of the Console class.

Syntax

public static Console console()
{
-------
------
}

Some common public method used in the Console class are:

  1. String readLine(): reads a single line of text from the console.
  2. String readLine(String formt, Object....args): reads the single line of text from the console.
  3. char() readPassword(): reads a password that is not displayed on the console.
  4. char[] readPassword(String formt, Object....args): reads a password that is not displayed on the console.

1.Examples

In this example, we make a class using console that reads the name of the user and prints it.

import java.io.*;
class ConsoleEx
 
{
   
public static void main(String args[])
     
{
       
Console con=System.console();
       
System.out.println("Whats your name");
       
String str=con.readLine();
       
System.out.println("Welcome " +str+ " to the java world");
     
}
  }

Output

fig-3.jpg

2. Example

In this example, we obtain the instance of the Console class that reads a password but when we type the password it is shown blank but in the output it shows our password.

import java.io.*;
class ConsoleEx2
  {
    public static void main(String args[])
      {
        Console con=System.console();
        System.out.println("Enter password-Any type whichever you want");
        char[] chr=con.readPassword();
        System.out.println("Now your password is");
        for(char ch1:chr)
        System.out.print(ch1);
      }
  }

Output

fig-4.jpg

2. InputStreamReader class

The InputStreamReader class is also used to read (or get) data from the keyboard. It mainly performs the following two tasks:

  • changes the byte-oriented stream into character-oriented stream.
  • provide a connection to input stream of keyboard.

BufferedReader class

The BufferedReader class can be used to read data line by line, using the readline() method.

1. Example

In this example, we are using both classes, used for getting data line by line.

import java.io.*;
class InpReaderEx   
 
{
   
public static void main(String[] args) throws Exception
     
{
       
InputStreamReader inpred=new InputStreamReader(System.in);
       
BufferedReader bffr=new BufferedReader(inpred);
       
System.out.println("Whats your name");
       
String sname=bffr.readLine();
       
System.out.println("welcome " + sname + " to the java world");
     
}
  }

Output

fig-1.jpg

When we run our program, it asks for user input (whats your name). In this example we provide "John" as input. Now the output is:

fig-2.jpg

 3. Scanner class

We have seen that there are various ways to read input from the keyboard, java.Util.Scanner is one of them. This class broke the data into tokens using delimiter.

Some common methods are:

  • String next(): returns the next token from the scanner.
  • String nextLine(): moves the scanner position to the next line and returns the value as a string.
  • byte nextByte(): it scans the next token as a byte.
  • short nextShort();
  • int nextInt():
  • long nextLong():
  • float nextFloat();
  • double nextDouble();

Example

import java.util.Scanner;
class ScannerEx
 
{
   
public static void main(String args[])
     
{
       
Scanner scnr=new Scanner(System.in);
       
System.out.println("Whats your rollno");
       
int rllnum=scnr.nextInt();
       
System.out.println("Whats your name");
       
String sname=scnr.next();
       
System.out.println("Your Rollno:" + rllnum+ " name:" + sname);
     
}
  }

Output

fig-5.jpg

4. DataInputStream class

The DataInputStream class reads primitive data types from an input stream. Applications use a data output stream to write data that can later be read by a data input stream.

A sample use of the constructor of the InputStream class is:

InputStream in = DataInputStream (InputStream in);

Some common public methods are:

  • final int read (byte[] s, int off, int len) throws IOException:
  • final int read (byte [] bt) throws IOException
  • to read the bytes:
       (a) final Boolean readBooolean() throws IOException,
       (b) final byte readByte() throws IOException,
       (c) final short readShort() throws IOException
       (d) final Int readInt() throws IOException
  • String readLine() throws IOException

Up Next
    Ebook Download
    View all
    Learn
    View all