How To Take Input from User in Java?

Java programming has various I/O packages to perform Input-Output operations. Unlike other programming languages, taking input from users is a little difficult in Java. 


There are multiple ways to take all different kinds of input from the user or from a file. These are:

  • Scanner Class.
  • BufferedReader Class.
  • Console Class.

1. Scanner Class.
We use the Scanner class to take input from the user through the command line. It has predefined functions to take different data types as input like integer, character, float, double, etc. 

Syntax:
We need to import the Scanner class to use the Scanner function.
  • import java.util.Scanner;
We need to create a new Scanner object called Scanner.
  • Scanner scan = new Scanner(System.in);

Example Code Implementation:

//Java program to take input from user using Scanner class
import java.util.Scanner;

public class InputExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = scanner.nextLine();

        System.out.print("Enter your age: ");
        int age = scanner.nextInt();

        System.out.println("Hello, " + name + ". You are " + age + " years old.");
    }
}
Output:
Enter your name: Probin
Enter your age: 24
Hello, Probin. You are 24 years old.

In this example, we first import the Scanner class and create a new Scanner object called a scanner. We then use the System.out.print method to prompt the user to enter their name, and then use the nextLine method of the Scanner class to read in the input as a string.

We then do the same thing for the user's age, using the nextInt method to read in the input as an integer.

Java Scanner Class Methods List.

Method Description
next() It is used to read the next token (word) from the input as a String.
nextLine() It is used to read the next line of input as a String.
nextInt() It is used to read the next token (integer) from the input as an int.
nextDouble() It is used to read the next token (a floating-point number) from the input as a double.
nextFloat() It is used to read the next token of the input as a float.
nextByte() It is used to read the next token of the input as a byte.
hasNext() It returns true if there is another token in the input (i.e. the end of the input has not been reached).
hasNextLine() It returns true if there is another line of input (i.e. the end of the input has not been reached).
useDelimiter(String pattern) It Sets the delimiter for the input to the specified regular expression pattern.

Note: The Scanner class can also be used to read input from files or other sources, not just from the command line.

2. BufferedReader Class.

The BufferedReader class is a simple class that is used to read a sequence of characters. It is used to read the data line by line using the readLine() method. The BufferedReader class is faster and more efficient for reading a large amount of input. 

Example Code Implementation.
//Java code to take input from user using BufferedReader class
import java.io.*;

public class InputExample {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter your name: ");
        String name = reader.readLine();

        System.out.print("Enter your age: ");
        int age = Integer.parseInt(reader.readLine());

        System.out.println("Hello, " + name + ". You are " + age + " years old.");
    }
}
Output:
Enter your name: Kumar
Enter your age: 24
Hello, Kumar. You are 24 years old.

In the above example, we are using the InputStreamReader() method that converts the input stream of bytes into a stream of characters.

3. Console class

Console class in Java is a built-in method for reading and writing text from the console. It is more secure for reading sensitive data like passwords without echoing them to the console.

List of some commonly used methods of console class:
Method Description
readLine() Reads a line of text from the console and returns it as a string.
readPassword() Reads a line of text from the console, but doesn't echo the characters to the screen.
printf(string format, Object... args) Writes formatted text to the console, similar to System.out.printf().
writer() Returns a PrintWriter object that can be used to write text to the console.

Example Code Implementation.
//Java code for taking user input using console class
import java.io.Console;

public class InputExample {
    public static void main(String[] args) {
        Console console = System.console();

        System.out.print("Enter your name: ");
        String name = console.readLine();

        System.out.print("Enter your age: ");
        int age = Integer.parseInt(console.readLine());

        System.out.println("Hello, " + name + ". You are " + age + " years old.");
    }
}
Output:
Enter your name: Jhon
Enter your age: 18
Hello, Jhon. You are 18 years old.

Note: Consoles class is not available in all IDEs and web applications so in that case you can use either Sanner or BufferedReader class for reading input from the user.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS