ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Scanner Class in Java

Scanner is a concrete class in Java. Hence, we can create object of Scanner class using new operator.

Scanner sc= new Scanner (System.in);

It is developed inside java.util package and was introduced in JDK 1.5 . It is having one constructor which required input stream through System.in .

 

Let us take a simple example :

  1. import java.util.Scanner ;
  2.  
  3. class DemoScanner{
  4.  
  5. public static void main (String[] args){
  6.  
  7. Scanner sc=new Scanner(System.in);
  8.  
  9. System.out.println("Enter something");
  10.  
  11. String s1=sc.next();
  12.  
  13. System.out.println("You have entered: "+s1);
  14.  
  15. }
  16.  
  17. }
  18.  
  19. next() is a method inside Scanner class , whenever this line is executed, the execution will halt there and it require some input through System.in. (keyboard )
  20.  
  21. Few other methods inside Scanner class are: nextInt() , nextBoolean() , nextDouble (), etc..

 

Depending on the input from the keyboard , respective method are used.

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!