Java Scanner class

By | June 25, 2023

The Java Scanner class is a powerful, flexible tool that can be used to sort and manipulate user input or data from various sources. It’s part of the Java course. Several methods to read various data types, such as integer, float, string, and more, are provided in the util package. The developers are able to easily access and manipulate user input or data by creating an instance of the scanner class, which is associated with inputs from sources such as a console or file.

 This method enables developers to draw specific data types from an input stream to process user inputs and files effectively. In addition, regular expressions are supported by the scanner class and can be used to improve pattern matching during input processing.

In summary, the Java Scanner class is a valuable tool for interactive programs and applications that need input from users or foreign sources to process data.

JAVA Scanner class input types

 

SN Modifier & Type Method Description
1) void close() It is used to close this scanner.
2) pattern delimiter() It is used to get the Pattern that the Scanner class is currently using to match delimiters.
3) Stream<MatchResult> findAll() It is used to find a stream of match results that match the provided pattern string.
4) String findInLine() It is used to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
5) string findWithinHorizon() It is used to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
6) boolean hasNext() It returns true if this scanner has another token in its input.
7) boolean hasNextBigDecimal() It is used to check if the next token in this scanner’s input can be interpreted as a BigDecimal using the nextBigDecimal() method or not.
8) boolean hasNextBigInteger() It is used to check if the next token in this scanner’s input can be interpreted as a BigDecimal using the nextBigDecimal() method or not.
9) boolean hasNextBoolean() It is used to check if the next token in this scanner’s input can be interpreted as a Boolean using the nextBoolean() method or not.
10) boolean hasNextByte() It is used to check if the next token in this scanner’s input can be interpreted as a Byte using the nextBigDecimal() method or not.
11) boolean hasNextDouble() It is used to check if the next token in this scanner’s input can be interpreted as a BigDecimal using the nextByte() method or not.
12) boolean hasNextFloat() It is used to check whether the next token in this scanner’s input can be interpreted as a Float using the nextFloat() method.
13) boolean hasNextInt() It is used to check if the next token in this scanner’s input can be interpreted as an int using the nextInt() method or not.
14) boolean hasNextLine() It is used to check if there is another line in the input of this scanner or not.
15) boolean hasNextLong() It is used to check whether the next token in this scanner’s input can be interpreted as a Long using the nextLong() method.
16) boolean hasNextShort() It is used to check if the next token in this scanner’s input can be interpreted as a Short using the nextShort() method or not.
17) IOException ioException() It is used to get the IOException last thrown by this Scanner’s readable.
18) Locale locale() It is used to get a Locale of the Scanner class.
19) MatchResult match() It is used to get the match result of the last scanning operation performed by this scanner.
20) String next() It is used to get the next complete token from the scanner which is in use.
21) BigDecimal nextBigDecimal() It scans the next token of the input as a BigDecimal.
22) BigInteger nextBigInteger() It scans the next token of the input as a BigInteger.
23) boolean nextBoolean() It scans the next token of the input into a boolean value and returns that value.
24) byte nextByte() It scans the next token of the input as a byte.
25) double nextDouble() It scans the next token of the input as a double.
26) float nextFloat() It scans the next token of the input as a float.
27) int nextInt() It scans the next token of the input as an Int.
28) String nextLine() It is used to get the input string that was skipped of the Scanner object.
29) long nextLong() It scans the next token of the input as a long.
30) short nextShort() It scans the next token of the input as a short.
31) int radix() It is used to get the default radix of the Scanner use.
32) void remove() It is used when this implementation of the Iterator does not support the remove operation.
33) Scanner reset() It is used to reset the Scanner in use.
34) Scanner skip() It skips input that matches the specified pattern, ignoring delimiters
35) Stream<String> tokens() It is used to get a stream of delimiter-separated tokens from the Scanner object which is in use.
36) String toString() It is used to get the string representation of Scanner using.
37) Scanner useDelimiter() It is used to set the delimiting pattern of the Scanner, which is in use to the specified pattern.
38) Scanner useLocale() It is used to sets this scanner’s locale object to the specified locale.
39) Scanner useRadix() It is used to set the default radix of the Scanner, which is in use to the specified radix.

 

Points To Ponder: JAVA Scanner Class

The Java Scanner class can read user inputs or data from other sources. We typically use System.In, which is the standard input stream for creating a Scanner object. We can pass an object of the file class if we need to read input from a file.

 We’ll use functions such as nextXYZ()+ to read numerical values for a particular data type. The nextShort() method is used to read a brief value, e.g., We’re using nextLine()+to read strings. Next().charAt(0), where next() returns the next word in the input as a string, and charAt(0) gives us the first character of that string if we want to read a single character.

The scanner class begins by reading a whole line and breaking it into smaller bits, which are referred to as tokens. The Java compiler uses token elements as meaningful elements. For example, if you have an input string like “How are you,” the scanner object will read and then write a token to each line: “how,” “what,” and “you.” Then, using various methods the Scanner class provides, we can iterate over each token and read it.

Examples

1)

import java.util.*;  

public class ScannerExample {  

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(“Name is: ” + name);             

          in.close();             

          }  

}  

 

Output:

Enter your name: Rahul Sharma

Name is: Rahul Sharma

2) 

import java.util.*;  

public class ScannerClassExample2 {    

      public static void main(String args[]){                       

          String str = “Hello/This is JavaTpoint/My name is Priya.”;  

          //Create scanner with the specified String Object  

          Scanner scanner = new Scanner(str);  

          System.out.println(“Boolean Result: “+scanner.hasNextBoolean());            

          //Change the delimiter of this scanner  

          scanner.useDelimiter(“/”);  

          //Printing the tokenized Strings  

          System.out.println(“—Tokenizes String—“);   

        while(scanner.hasNext()){  

            System.out.println(scanner.next());  

        }  

          //Display the new delimiter  

          System.out.println(“Delimiter used: ” +scanner.delimiter());            

          scanner.close();  

          }    

}  

Output:

Boolean Result: false

—Tokenizes String—

Hello

This is JavaTpoint

My name is Priya.

The delimiter used: /

Frequently Asked Questions

Q1. What is the scanner class in Java?

Ans. The Java scanner classes are part of Java. Util package. This was released as part of the Java 1.5 release. User input is mostly received by scanners and processed into basic types of data, for example, int, double, or standard string. By generating tokens, it is a utility class for the processing of data using regular expressions.

Q2. How to get a scanner class in Java?

Ans.  Input method for Java’s Scanner String Import Java.Util.*, so that you can access the Scanner class of Java. To create an instance of the Scanner class, use the new keyword. The Static System. An object must be passed to the scanner’s constructor.

Q3. What is scanner data used for?

Ans.  For a certain period of time, usually a week or a month, scanner data sets contain quantities of products sold and revenue received by the retailer for these products. This information allows national service organizations to determine the price of a single product on the basis of an estimate of its revenue broken down by quantities sold.

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe
Scroll to Top
close
counselling
Want to Enrol in PW Skills Courses
Connect with our experts to get a free counselling & get all your doubt cleared.