Scanner Class in Java is pre-defined class provided by java language version 5 and later. This class is useful to input the different types of values. This scanner class does not work individually, but it contains system.in which is used for standard input. It is provided under java.util package.

Syntax

Scanner obj=new Scanner(System.in)
Syntax of Scanner Class

Example of Scanner Class in Java

Input Value

import java.util.*;  
public class Scannerf {  
public static void main(String args[]){  
          Scanner in = new Scanner(System.in);  
          System.out.print("Enter your name: ");  
          String name = in.nextLine();  
          System.out.println("My Name is: " + name);             
          in.close();             
          }  
}  
Scanner Class

Let’s run the code

Input Value

Output Value

Output Value

Method of Scanner Class in Java

  • nextInt() :- to input int type value.
  • Nextfloat() :- to input float type value.
  • nextChar() :- to input Char type value.
  • nextLong() :- to input Long type value.
  • nextDouble() :- to input Double type value.
  • nextByte() :- to input Byte type value.
  • nextLine() :- to input String type value.

Methods of system.in

  • read():- This method will take a character input (byte) from keyboard.
  • readLine():- This method is used for taking input from keyboard as string.

Stream classes

These the pre-defined classes that represent. The flow of the data from specific location/device.

DataInputStream

This class is the stream class provided by java which can be use for defining stream that will take input from standard input device such as a keyboard.

Syntax of DataInputStream

DataInputStream obj.new DataInputStream(System.in);
Syntax of DataInputStream

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

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