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.
- import java.util.Scanner;
- Scanner scan = new Scanner(System.in);
//Java program to take input from user using Scanner classimport 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."); } }
Enter your name: Probin
Enter your age: 24
Hello, Probin. You are 24 years old.
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. |
2. BufferedReader Class.
//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."); } }
Enter your name: Kumar
Enter your age: 24
Hello, Kumar. You are 24 years old.
3. 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. |
//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."); } }
Enter your name: Jhon
Enter your age: 18
Hello, Jhon. You are 18 years old.



