KnowledgeBoat Logo

Computer Applications

Write syntax along with an example to create a scanner object.

Input in Java

28 Likes

Answer

Syntax to create a Scanner object is:

Scanner sc = new Scanner (System.in);

Here, sc is an object of Scanner type.

For example, the below program creates an object of Scanner class and uses it to input an integer value:

import java.util.*;
class KboatScannerExample
{
    public static void main(String args[])
    {
        int num;
        Scanner sc = new Scanner(System.in);
        num = sc.nextInt();
        System.out.println(num);
    }
}

Answered By

18 Likes


Related Questions