ways-to-read-input-from-console-in-java

Ways to read input from console in Java

In Java, there are three distinct ways for perusing contribution from the client in the order line environment(console).

1.Using Buffered Reader Class

This is the Java old style technique to take input, Introduced in JDK1.0. This technique is utilized by wrapping the System.in (standard info stream) in an InputStreamReader which is enclosed by a BufferedReader, we can peruse contribution from the client in the order line.

Advantages

  • The input is buffered for efficient reading.

Drawback:

  • The wrapping code is hard to remember.

Program:

// Java program to demonstrate BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test 
{
    public static void main(String[] args) throws IOException 
    {
        //Enter data using BufferReader
        BufferedReader reader = 
                   new BufferedReader(new InputStreamReader(System.in));
        
        // Reading data using readLine
        String name = reader.readLine();
 
        // Printing the read line
        System.out.println(name);        
    }
}

Input:

Geek

Output:

Geek

Note: To read other types, we use functions like Integer.parseInt(), Double.parseDouble(). To read multiple values, we use split().

2. Using Scanner Class

This is likely the most favored technique to take input. The fundamental motivation behind the Scanner class is to parse crude sorts and strings utilizing ordinary articulations, anyway it is likewise can be utilized to peruse contribution from the client in the order line.

Favorable circumstances:

  • Advantageous techniques for parsing natives (nextInt(), nextFloat(), … ) from the tokenized input.
  • Ordinary articulations can be utilized to discover tokens.

Disadvantage:

  • The reading methods are not synchronized

To see more differences, please see this article.

// Java program to demonstrate working of Scanner in Java
import java.util.Scanner;
 
class GetInputFromUser
{
    public static void main(String args[])
    {
        // Using Scanner for Getting Input from User
        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);
    }
}

Input:

GeeksforGeeks
12
3.4

Output:

You entered string GeeksforGeeks
You entered integer 12
You entered float 3.4

3. Using Console Class

It has been turning into a favored route for perusing client’s contribution from the order line. Furthermore, it very well may be utilized for perusing secret word like contribution without repeating the characters entered by the client; the organization string punctuation can likewise be utilized (like System.out.printf()).

Favorable circumstances:

  • Perusing secret phrase without repeating the entered characters.
  • Perusing strategies are synchronized.
  • Configuration string grammar can be utilized.

Drawback:

  • Does not work in non-interactive environment (such as in an IDE).
// Java program to demonstrate working of System.console()
// Note that this program does not work on IDEs as 
// System.console() may require console
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);
    }
}

If it’s not too much trouble refer this for all the more quicker methods of understanding information.

This article is contributed by D Raj Ranu. On the off chance that you like GeeksforGeeks and might want to contribute, you can likewise compose an article utilizing contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article showing up on the GeeksforGeeks principle page and help different Geeks.

It would be ideal if you compose remarks in the event that you discover anything erroneous, or you need to share more information about the point examined previously.

Consideration peruser! Try not to quit adapting now. Get hold of the relative multitude of important Java Foundation and Collections ideas with the Fundamentals of Java and Java Collections Course at an understudy cordial cost and become industry prepared.