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
Syntax of Scanner ClassScanner obj=new Scanner(System.in)
Example of Scanner Class in Java
Input Value
Scanner Classimport 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(); } }
Let’s run the code
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
Syntax of DataInputStreamDataInputStream obj.new DataInputStream(System.in);
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
0 Comments