Scanner class in Java

In this tutorial we will learn about Scanner Class in Java. Scanner class in Java is use to get input from use as we use scanf() in c. Scanner class is predefined class in java which is present in java.util package. It mean we have to import this package to use Scanner class in Program. In this tutorial we will learn how to use Scanner class and different methods of Scanner class.

Scanner Class in Java


Here I would like to start directly with an example which would so helpful to you to understand Scanner class.

Example
 
import java.util.Scanner;  // Import the Scanner class

class ScannerExample {
  public static void main(String[] args) {
    Scanner obj = new Scanner(System.in);  // Creating object of Scanner class
    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.

MethodDescription
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



Share this tutorial with your friends on your social Media..

Scanner class in Java Scanner class in Java Scanner class in Java Scanner class in Java



Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.


Owlbuddy.com