Java – Scanner Class

Scanner Class:

  • Using java library class Scanner, we can read input like integers, characters, strings, double values from the user.
  • Different methods to read different values such as nextInt(), next(), nextDouble()…
  • We need to specify the Keyboard(System.in) while creating Scanner class object.
    • Scanner scan = new Scanner(System.in);
  • We access all the methods using object reference name.

Reading integer value: nextInt() method read and returns an integer value

import java.util.Scanner;
public class Practice
{
            public static void main(String[] args)
            {
                        Scanner scan = new Scanner(System.in);
                        System.out.print(“Enter integer value : “);
                        int n = scan.nextInt();
                        System.out.println(“Input value is : ” + n);
            }
}

Reading int, double and boolean:

import java.util.Scanner;
public class Practice
{
            public static void main(String[] args)
            {
                        Scanner scan = new Scanner(System.in);
                        System.out.print(“Enter Emp ID : “);
                        int id = scan.nextInt();
   
                        System.out.print(“Enter Emp salary : “);
                        double salary = scan.nextDouble();
   
                        System.out.print(“Enter Emp maritual state : “);
                        boolean married = scan.nextBoolean();
   
                        System.out.println(“Emp details are : ” + id + ” , ” + salary + ” , ” + married);
            }
}

Reading String: using next() method we can read single word string from the user

import java.util.Scanner;
public class Practice
{
            public static void main(String[] args)
            {
                        Scanner scan = new Scanner(System.in);
                        System.out.print(“Enter your name : “);
                        String name = scan.next();
   
                        System.out.println(“Hello ” + name);
            }
}

Reading character: There is no method in Scanner class to read single character, hence we read first character of String to read single character as follows

import java.util.Scanner;
public class Practice
{
            public static void main(String[] args)
            {
                        Scanner scan = new Scanner(System.in);
                        System.out.print(“Enter character : “);
                        char ch = scan.next().charAt(0);
   
                        System.out.println(“Input character is : ” + ch);
            }
}

Reading Employee details:

import java.util.Scanner;
public class Practice
{
            public static void main(String[] args)
            {
                        Scanner scan = new Scanner(System.in);
                        System.out.print(“Enter emp details(id, name, gender, salary, married : “);
   
                        int id = scan.nextInt();
                        String name = scan.next();
                        char gender = scan.next().charAt(0);
                        double salary = scan.nextDouble();
                        boolean married = scan.nextBoolean();
   
                        System.out.println(“Details are : ” + id + “, ” + name + ” , ” + gender + ” , ” + salary + ” , ” + married);
            }
}
Scroll to Top