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 passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​