In this tutorial, we will learn about Scanner Class in Java. Scanner class in Java is used to get input from users as we use scanf() in C language. Scanner class is a predefined class in java which is present in java.util package. It means we have to import this package to use Scanner class in Program. Here we will learn how to use Scanner class and different methods of Scanner class.
Here I would like to start directly with an example which would so helpful to you to understand Scanner class.
// Import the Scanner class
import java.util.Scanner;
class ScannerExample {
public static void main(String[] args) {
// Creating object of Scanner class
Scanner obj = new Scanner(System.in);
System.out.println("Enter First Number");
int num1= obj.nextInt(); // Reading user input
System.out.println("Enter Second Number");
int num2= obj.nextInt(); // Reading user input
System.out.println("Sum is: " + (num1+num2));
}
}
Here we will check available methods in Scanner class.
| Method | Description |
|---|---|
| nextBoolean() | Read boolean input |
| nextByte() | Read byte input |
| nextDouble() | Read double input |
| nextFloat() | Read float value |
| nextInt() | Read int input |
| nextLine() | Read string input |
| nextLong() | Read long value |
| nextShort() | Read short input |
Parvesh Sandila is a results-driven tech professional with 8+ years of experience in web and mobile development, leadership, and emerging technologies.
After completing his Master’s in Computer Applications (MCA), he began his journey as a programming mentor, guiding 100+ students and helping them build strong foundations in coding. In 2019, he founded Owlbuddy.com, a platform dedicated to providing free, high-quality programming tutorials for aspiring developers.
He then transitioned into a full-time programmer, where his hands-on expertise and problem-solving skills led him to grow into a Team Lead and Technical Project Manager, successfully delivering scalable web and mobile solutions. Today, he works with advanced technologies such as AI systems, RAG architectures, and modern digital solutions, while also collaborating through a strategic partnership with Technobae (UK) to build next-generation products.
