Java User Input
The most common way to take user input in Java is using Scanner class which is part of java.util package. The scanner class can read input from various sources like console, files or streams. This class was introduced in Java 5. Before that we use BufferedReader class(Introduced in Java 1.1). As a beginner, we will suggest to use Scanner class.
Follow these steps to take user input using Scanner class:
- Import the Scanner class using import java.util.Scanner;
- Create the Scanner object and connect Scanner with System.in by passing it as an argument i.e. Scanner scn = new Scanner(System.in);
- Print a message to prompt for user input and you can use the various methods of Scanner class like nextInt(), nextLine(), next(), nextDouble etc according to your need.
Example 1: Here, we are taking integer inputs from the user.
// taking user input
import java.util.Scanner;
class Geeks
{
public static void main(String[] args)
{
// Creating Scanner class object
Scanner scn = new Scanner(System.in);
// Enter first input
System.out.print("Enter First Number: ");
int a = scn.nextInt();
System.out.print("Enter Second Number: ");
int b = scn.nextInt();
System.out.println("Sum: " + (a + b));
// Closing the scanner to release resources
scn.close();
}
}
Output:
Enter First Number: 2
Enter Second Number: 3
Sum: 5
Scanner class provides some methods to read different data types:
Method | Description |
---|---|
Used for reading Boolean value. | |
Used for reading Byte value. | |
Used for reading Double value. | |
Used for reading Float value. | |
Used for reading Int value. | |
Used for reading Line value. | |
Used for reading Long value. | |
Used for reading Short value. |
Example 2: Here, we are taking multiple user inputs like string, float, double etc. and using the above mentioned methods of Scanner Class.
// taking multiple user input using Scanner Class
import java.util.Scanner;
class Geeks
{
public static void main(String[] args)
{
// Scanner definition
Scanner scn = new Scanner(System.in);
// input is a string(one word)
// read by next() method
String str1 = scn.next();
// print string
System.out.println("Entered String str1: " + str1);
// input is a string(complete Sentence)
// read by nextLine() method
String str2 = scn.nextLine();
// print string
System.out.println("Entered String str2: " + str2);
// input is an integer
// read by nextInt() method
int x = scn.nextInt();
// print integer
System.out.println("Entered Integer: " + x);
// input is a floatingValue
// read by nextFloat() method
float f = scn.nextFloat();
// print floating value
System.out.println("Entered FloatValue: " + f);
}
}
Output:
Entered String str1 : Geeks
Entered String str2 : Geeks For Geeks
Entered Integer : 123
Entered FloatValue : 123.090
Another method to take user input is using BufferedReader Class. It is a simple class that is used to read a sequence of characters. It provides several methods, including:
- read(): Reads a single character.
- read(char[] cbuf): Reads an array of characters.
- readLine(): Reads an entire line of text.
To know more about BufferedReader Class, please refer to the article – BufferedReader Class in Java
BufferedReader vs Scanner Class
Aspects | BufferedReader | Scanner |
---|---|---|
Primary Use | Efficient reading of character streams. | Reading formatted input (e.g., integers, strings). |
Speed | Faster for large input as it does less parsing. | Slower due to parsing overhead (e.g., nextInt() , nextFloat() ). |
Exception Handling | Throws checked exceptions (e.g., IOException ). | No checked exceptions; easier to use. |
Flexibility | Allows reading larger input efficiently. | Best suited for reading simple data types. |
Thread Safety | Synchronized, making it thread-safe. | Not thread-safe by default. |
Common Use | Used for reading large input efficiently. | Commonly used for smaller, formatted input. |
FAQs – Java User Input
What are the different ways to take input in Java?
Methods to take input in Java as mentioned below:
- BufferedReader
- Scanner
- Console
Is Scanner
thread-safe?
No, Scanner is not thread-safe by default. If you’re using it in a multi-threaded environment, you may need to synchronize access or use a different approach (like BufferedReader with explicit synchronization).
What happens when we use nextLine()
after nextInt()
or nextFloat()
?
When you use
nextInt()
ornextFloat()
, it reads the number but leaves the newline character in the input buffer. If you callnextLine()
right after, it will read this newline as input instead of a new line.Follow this resolve such cases:
Scanner sc = new Scanner(System.in);
// Reads integer
int num = sc.nextInt();
// Consume newline character
sc.nextLine();
// Reads the next line properly
String name = sc.nextLine();