Scanner in java

Java Scanner class

  • Java Scanner class is used to read input from keyboard. Scanner class is find in java.util.Scanner package.
  • Java Scanner class breaks the input into tokens using a delimiter that is white space by default.
  • Java Scanner class is widely used to parse text for string and primitive types using regular expression.
  • Java Scanner class extends Object class and implements Iterator and Cloneable interfaces.

Steps to Read Input From User:

Step 1:  import java.util.Scanner;

Step 2:  Create a Scanner class object sc1.

Scanner sc1=new Scanner (; standard input device

Step 3: Use below methods to read inputs.

Methods to read different types of value from Java Scanner class

  • sc.nextInt() : It is used to read integer value from keyboard return type of this method is integer.
  • sc.nextDouble() : It is used to read double value from keyboard ,return of this method is double
  • : It is used to read string value from keyboard ,return of this method is string .
  • sc.nextFloat() :  It is used to read string value from keyboard ,return of this method is string .

Program to take input from keyboard

[pastacode lang=”java” manual=”import%20java.util.Scanner%3B%0A%0Aclass%20Pro11%0A%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Scanner%20sc%3Dnew%20Scanner(!%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Enter your Name:
Kundan kumar
Enter Your age:
eligible for vote

[pastacode lang=”java” manual=”import%20java.util.*%3B%20%20%0Apublic%20class%20ScannerTest%7B%20%20%0Apublic%20static%20void%20main(String%20args%5B%5D)%7B%20%20%0A%20%20%20%20%20String%20input%20%3D%20%2210%20tea%2020%20coffee%2030%20tea%20buiscuits%22%3B%20%20%0A%20%20%20%20%20Scanner%20s%20%3D%20new%20Scanner(input).useDelimiter(%22%5C%5Cs%22)%3B%20%20%0A%20%20%20%20%20System.out.println(s.nextInt())%3B%20%20%0A%20%20%20%20%20System.out.println(″ message=”Java Scanner Example with delimiter” highlight=”” provider=”manual”/]



Leave a Reply