In java, there are different ways to accept input from user, we can also accept input from user in java using scanner class. scanner class is a predefined class in java.util package, and we can type cast input in different data type.
// Java program to accept input from user using Scanner class.
import java.util.Scanner;
class GetInput
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int a,b,c;
a = in.nextInt();
b = in.nextInt();
c=a+b;
System.out.println("SUM : "+c);
}
}
The Scanner class in Java is used to take input from the user during program execution. It is one of the most commonly used classes for beginner Java programs because it makes input handling simple and easy to understand.
Using the Scanner class, programmers can accept different types of input such as integers, strings, floating-point numbers, and characters.
This article explains how to accept input from the user using the Scanner class in Java with syntax, examples, output, and explanation.
What is Scanner Class in Java?
The Scanner class is a predefined class available in the java.util package.
It is used to read user input from:
- Keyboard
- Files
- Input streams
The Scanner class provides multiple methods to accept different data types from the user.
Importing Scanner Class
Before using the Scanner class, the following package must be imported.
import java.util.Scanner;
Syntax of Scanner Class
Scanner sc = new Scanner(System.in);
Explanation
Scanner→ Class namesc→ Object namenew→ Creates objectSystem.in→ Takes input from keyboard
Java Program to Accept Integer Input
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number;
System.out.print("Enter a number: ");
number = sc.nextInt();
System.out.println("Entered number is: " + number);
}
}
Output
Enter a number: 25
Entered number is: 25
Java Program to Accept String Input
import java.util.Scanner;
public class StringInputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name;
System.out.print("Enter your name: ");
name = sc.nextLine();
System.out.println("Your name is: " + name);
}
}
Output
Enter your name: Rahul
Your name is: Rahul
Common Scanner Methods in Java
| Method | Description |
|---|---|
nextInt() | Reads integer value |
nextFloat() | Reads float value |
nextDouble() | Reads double value |
next() | Reads single word |
nextLine() | Reads complete line |
next().charAt(0) | Reads character |
How Scanner Class Works
The Scanner object reads data entered from the keyboard using System.in.
When the user enters input and presses Enter, the Scanner class stores the value in a variable.
Different methods are used for different data types.
For example:
nextInt()for integersnextLine()for stringsnextDouble()for decimal values
Advantages of Scanner Class
- Easy to use for beginners
- Supports multiple data types
- Built-in Java class
- Makes user interaction simple
- Commonly used in Java programs
Disadvantages of Scanner Class
- Slightly slower than some other input methods
- May cause input mismatch errors if wrong data type is entered
- Requires importing
java.util.Scanner
Important Note About Scanner Class
The Scanner class is one of the most widely used input methods in Java programming. However, programmers should properly close the Scanner object after use to avoid resource leaks in larger applications.
Example:
sc.close();
Difference Between next() and nextLine()
| Feature | next() | nextLine() |
|---|---|---|
| Reads Single Word | Yes | No |
| Reads Full Sentence | No | Yes |
| Stops at Space | Yes | No |
Frequently Asked Questions
What is Scanner class in Java?
The Scanner class is used to take input from the user during program execution.
Which package contains Scanner class?
The Scanner class belongs to the java.util package.
How do you take integer input in Java?
The nextInt() method is used to take integer input in Java.
Why is Scanner class used?
Scanner class is used to accept user input such as integers, strings, and decimal values.
Is Scanner class beginner-friendly?
Yes, the Scanner class is simple and beginner-friendly for learning Java input concepts.
Conclusion
The Scanner class in Java is a simple and powerful way to accept user input from the keyboard. It helps beginners understand input handling and user interaction in Java programs.
By learning the Scanner class, students can create interactive Java applications using different types of user input.
