Scanner Class in Java

0
157

This tutorial is all about 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 :
[java]

import java.util.Scanner ;

class DemoScanner{

public static void main (String[] args){

Scanner sc=new Scanner(System.in);

System.out.println(“Enter something”);

String s1=sc.next();

System.out.println(“You have entered: “+s1);

}

}

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 )

Few other methods inside Scanner class are: nextInt() , nextBoolean() , nextDouble (), etc..

[/java]

 

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

Facebook Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.