Thursday, 2 February 2017

SCANNER CLASS IN JAVA

Scanner is a special kind of class and most commonly used in java to take user input. It provides different methods which state the type of input. We don’t have to parse user input and print the output.

Example –
import java.util.Scanner;
public class ScannerClass {
          public static void main(String[] args) {
                   Scanner scan = new Scanner(System.in);
                   System.out.println("Enter the value of i ");
                   int i = scan.nextInt();
                   System.out.println("Enter the value of j ");
                   int j = scan.nextInt();
                   System.out.println("Enter your name ");
                   String name = scan.next();
                   System.out.println(i+j);
                   System.out.println(name);
          }
}

Output –
Enter the value of i
5
Enter the value of j
10
Enter your name
rakesh
15
rakesh


Note – For every data type Scanner class provide different methods.

No comments:

Post a Comment