scanner class in java

Scanner class in Java

Scanner class in java is found in java.util package and has a rich interface set which is needed to breakdown the input into small tokens. It takes the tokens and converts them into primitive data types using java regular expressions.  The input can also be broken down into tokens with some unique patterns such as blanks, tabs, lines etc.
To Know about Java String Split method

Syntax

public class Scanner S
extends Object
implements <String>

Below is the code on how to get a java scanner

Scanner in = new Scanner(System.in);

This is the simplest example to read an integer number using the scanner class in Java.

Scanner scanner =  new Scanner(System.in);
int number =  scanner.nextInt(); 

We can also create instances of this class to parse a character, a string or byte input stream using constructors. Some of them can be coded as:

  • Scanner (File Source): It results in scanning a specific file using default charset.
  • Scanner (File Source, String charsetName): It will construct a scanner to scan a specific file using specific charset.
  • Scanner (InputStream source): This instance will build a scanner from the byte input stream using specific charset.
  • Scanner (Readable source): It constructs a scanner from a character stream.
  • Scanner (String source): It constructs a scanner from a string.
  • Scanner (Path source): This constructs a scanner which will produce values for specific files.
  • Scanner (Path source, String charsetName): This will produce values from the scanned files.

There are also some pre-defined methods used in the scanner class for each of the data types.

intnextInt()
shortnextShort()
doublenextDouble()
floatnextFloat()
charnext().chatAt()
stringnextLine()

Below is a simple scanner class Demo for better understanding:

Scanner scanner = new Scanner(System.in);
 
System.out.print("Enter your name");
String name = scanner.next();
 
System.out.print("Enter your age");
int age = scanner.nextInt();
 
System.out.print("Enter your father’s name");
String fathername = scanner.next(); 
 
System.out.println("My name is: " + name);
System.out.println("My age is: " + age);
System.out.println("My father’s name is: " + fathername);
 
scanner.close();

Here is an example for better understanding of Java scanner

import java.util.*;  
public class ScannerClassMyself {    
public static void main(String args[]){                       
String s = "Hey, This is Developer Helps.";   
//This is to create scanner Object and pass a string in it  

Scanner scan = new Scanner(s);  // This is to check if the scanner has a token  
System.out.println("Boolean Result: " + scan.hasNext());  
System.out.println("String: " +scan.nextLine());  
//This is to print the string  
scan.close();              //This is to close the scan 

System.out.println("Please Enter Your Details");  
Scanner in = new Scanner(System.in);  
System.out.print(" Please fill in your name: ");    
String name = in.next();   
System.out.println("Name: " + name);           
System.out.print("Please fill in your age: ");  
int x = in.nextInt();  
System.out.println("Age: " + x);  
System.out.print("Please fill in your salary: ");  
double d1 = in.nextDouble();  
System.out.println("Salary: " + d1);         
in.close();           
}    
}  

The output of the program is:

Boolean Result: true
String: Hey, This is Developer Helps.

Please Enter Your Details:
Please fill in your name: Megha
Name: Megha
Please fill in your age: 25
Age: 25
Please fill in your salary: 20000
Salary: 20000.0

Thanks for reading the post. If you have faced any issue in the post, please do comment. Because of your comment, we can improve the content and provide the best material in the future. So please like and share the post.

Leave a comment

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