| by Arround The Web | No comments

How to Get User Input in Java

In programming languages, taking the user’s input is an essential task. In Java, multiple predefined classes are used to get the user’s input such as Scanner, BufferedReader, and Console class. All these classes utilizes various methods for handling input such as nextLine(), readLine(), etc.

This post will explain the below-listed ways of getting the user’s input:

  • What is Java Scanner Class and How to Use it in Java.
  • What is Java BufferedReader Class and How to Use it in Java.
  • What is Java Console Class and How to Use it in Java.

So let’s begin!

What is Java Scanner Class and How to Use it in Java

Java Scanner is the most popularly used class to get the user’s input in java. It provides various synchronized methods that are used to read the input. The most significant feature of the Scanner class is that it can take the input from various sources such as files, input streams, users, and so on.

Example
The code block given below will explain how to use Scanner class to get the string, and integer type data in java:

package javascanner;
import java.util.*;
public class JavaScanner {
  public static void main(String[] args) {
      Scanner scn = new Scanner(System.in);  
      System.out.println("Enter Student's Name: ");
      String stdName = sc.nextLine();
      System.out.println("Enter Student's Roll No: ");
      int rollNo = sc.nextInt();
      System.out.println("Student Name :" + stdName);
      System.out.println("Student Roll Number:" + rollNo);
   }
}

In the above java program, we performed the below-listed functionalities:

  • We utilized the nextLine() method to get the String data, i.e., the student’s name.
  • After that, we utilized the nextInt() method to get the integer data, i.e., the student’s roll number.
  • Finally, we printed the user-entered data using the System.out.println() statement.

The output shows that the scanner class successfully takes the input from the user.

What is Java BufferedReader Class and How to Use it in Java

Java provides a predefined class named the BufferedReader class used to get the user’s input. It is a classic java approach for getting the user’s input. It just reads the input stream and doesn’t perform any parsing.

Example
In this example, we will utilize the readLine() method of the BufferedReader Class to get the user’s input:

import java.io.*;
public class UserInput {
    public static void main(String[] args)
            throws IOException {
        BufferedReader input = new BufferedReader(
                new InputStreamReader(System.in));
        String data = input.readLine();
        System.out.println("Student's Name: " + data);
    }
}

The below snippet shows the complete code and its corresponding output:

The above snippet verified the working of the BufferedReader class.

What is Java Console Class and How to Use it in Java

It is a predefined class in java used to get the user’s input from the console(i.e., keyboard/screens). It provides several methods that are used for different purposes, such as the readLine() method is used to read the text from the console. The Console class doesn’t work in a non-interactive environment, e.g., IDE.

Example
The below-given code block will explain how to get user’s input from the console/keybord:

public class UserInput {
    public static void main(String[] args) {
        Console input = System.console();
        String stdName = input.readLine("Enter Your Name: ");
        System.out.println("Student's Name: " + stdName);
    }
}

Now, open the command prompt and follow the instruction provided in the below snippet:

This is how we compile and run a java program using Java Console class.

Conclusion

In java, multiple predefined classes like Scanner, BufferedReader, and Console class are used to get the user’s input. Java Scanner class parses the input data and utilizes various methods to get the user’s input. The BufferedReader class just reads the input stream and doesn’t perform any parsing. It utilizes various built-in methods to get the user’s input. While the Console class is used to get the user’s input from the console (i.e., keyboard/screens), it doesn’t work in a non-interactive environment, e.g., IDE.

Share Button

Source: linuxhint.com

Leave a Reply