How to Read Java Console Input | 3 Ways To Read Java Input

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

1. Objective

After studying literals in Java, let’s move towards a query, how to read Java console Input? In this Java tutorial, we are going to learn about what is Java console and different ways to read input from Java console by using Java bufferedreader class, scanner class in Java, and console Class in Java with their example and pros & cons.

So, let’s start to explore different ways to read input from the java console.

How to Read Input from Java Console

How to Read Input from Java Console

2. Java Console

There are three different ways to read the input from Java Console, they are –

  • Using Java Bufferedreader Class
  • Scanner Class in Java
  • Console Class in Java

So, let’s study them in detail.

Do you know What is Java Character Class Methods?

3. Read Result From Java Console

Here, we will cover three ways to read the result from console.

a. Java Bufferedreader Class

This is the Java traditional technique, introduced in JDK1.0. This strategy is utilized by wrapping the System.in (standard information stream) in an InputStreamReader which is wrapped in a Java BufferedReader, we can read result include from the user in the order line.

Pros –

The information is cradled for productive perusing.

Cons –

The wrapping code is difficult to recall.

Example of Java Bufferedreader Class–

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader reader =
                   new BufferedReader(new InputStreamReader(System.in));
        String name = reader.readLine();
        System.out.println(name);       
    }
}

b. Scanner Class in Java

This is presumably the most favoured technique to take input. The primary reason for the Scanner class is to parse primitive composes and strings utilizing general expressions, in any case, it can utilize to peruse contribution from the client in the order line.

Pros –

  • Helpful strategies for parsing natives (nextInt(), nextFloat(), … ) from the tokenized input.
  • General articulations can utilize to discover tokens.

Cons –

  • The reading methods are not synchronized.

Let’s Look at Java File Class – java.io.File Class in Java

Example of Scanner Class in Java –

import java.util.Scanner;
class GetInputFromUser
{
    public static void main(String args[])
    {
                Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        System.out.println("You entered string "+s);
        int a = in.nextInt();
        System.out.println("You entered integer "+a);
        float b = in.nextFloat();
        System.out.println("You entered float "+b);
    }
}

c. Console Class in Java

It has been turning into a favored route for perusing client’s contribution from the command line. In addition, it can utilize for password key like contribution without resounding the characters entered by the client, the configuration string syntax structure can likewise utilize (like System.out.printf()).

Pros –

  • Reading secret word without reverberating the entered characters.
  • Reading strategies that are synchronized.
  • Format string sentence structure can utilize.

Cons –

  • Does not work in non-intelligent condition, (for example, in an IDE).

Do You Know Difference Between Abstract Class and Interface in Java?

Example of Console Class in Java–

public class Sample
{
    public static void main(String[] args)
    {       
        // Using Console to input data from user
        String name = System.console().readLine();
        System.out.println(name);
    }
}
Java Quiz

4. Java Console Example

String name = null;
int number;
java.io.BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
name = in.readLine();number = Integer.parseInt(in.readLine());
System.out.println("Name " + name + "\t number " + number);
java.util.Scanner sc = new Scanner(System.in).useDelimiter("\\s");
name = sc.next(); 
number = sc.nextInt();System.out.println("Name " + name + "\t number " + number);
java.io.Console cnsl = System.console();
if (cnsl != null) {
    name = cnsl.readLine("Name: ");
    System.out.println("Name entered: " + name);
}

So, this was all about Java Console Tutorial. Hope you like our explanation.

Let’s Study Java Comments | Three Types of Comments in Java

5. Conclusion

Hence, in this Java console tutorial, we have learned three ways to read input from Java console, using Java bufferedreader class, scanner class in Java, and console class in Java. Moreover, we discussed these ways with console Input examples. At last, we saw the pros and cons of all the 3 ways to read input from console. Furthermore, if you have any query, feel free to ask in the comment section.

Related Topic- What is Autoboxing and Unboxing in Java 

For reference 

1 Response

  1. Rohith A says:

    i want r console output in java console

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.