•Scanner class is one of the class of Java which provides the methods to get inputs. •Scanner class is present in java.util package so we import this package in our program. •We first create an object of Scanner class and then we use the methods of Scanner class.
Scanner a = new Scanner(System.in); Following are methods of Scanner class: •next( ) to input a string •nextByte() to input a byte value •nextInt( ) to input an integer •nextFloat( ) to input a float •nextLong( ) for Long Values •nextDouble() for double values
import java.util.Scanner; class GetInput { public static void main(String args[]) { int a; Scanner in = new Scanner(System.in); System.out.println("Enter an integer"); a = in.nextInt(); System.out.println("You entered integer "+a); } }
import java.util.Scanner; class GetInput { public static void main(String args[]) { float b; Scanner in = new Scanner(System.in); System.out.println("Enter a float"); b = in.nextFloat(); System.out.println("You entered float "+b); } }
import java.util.Scanner; class GetInput { public static void main(String args[]) { String s; Scanner in = new Scanner(System.in); System.out.println("Enter a string"); s = in.nextLine(); System.out.println("You entered string"+s); } }

Handling inputs via scanner class

  • 1.
    •Scanner class isone of the class of Java which provides the methods to get inputs. •Scanner class is present in java.util package so we import this package in our program. •We first create an object of Scanner class and then we use the methods of Scanner class.
  • 2.
    Scanner a =new Scanner(System.in); Following are methods of Scanner class: •next( ) to input a string •nextByte() to input a byte value •nextInt( ) to input an integer •nextFloat( ) to input a float •nextLong( ) for Long Values •nextDouble() for double values
  • 3.
    import java.util.Scanner; classGetInput { public static void main(String args[]) { int a; Scanner in = new Scanner(System.in); System.out.println("Enter an integer"); a = in.nextInt(); System.out.println("You entered integer "+a); } }
  • 4.
    import java.util.Scanner; classGetInput { public static void main(String args[]) { float b; Scanner in = new Scanner(System.in); System.out.println("Enter a float"); b = in.nextFloat(); System.out.println("You entered float "+b); } }
  • 5.
    import java.util.Scanner; classGetInput { public static void main(String args[]) { String s; Scanner in = new Scanner(System.in); System.out.println("Enter a string"); s = in.nextLine(); System.out.println("You entered string"+s); } }