Understanding Java Scanner Class

Understanding Java Scanner Class

3 mins read324 Views Comment
clickHere
Updated on Oct 3, 2023 11:52 IST

In this article, you will learn about Java Scanner class. We have also covered Java user input, input types, etc., relevant topics. Let’s begin!

2023_02_Java-Scanner-Class.jpg

The java.util package contains a class named Scanner that is used to receive input of primitive types like int, double, etc., as well as strings. If you want an input method for situations where time is a factor, like in competitive programming, it is the simplest way to read input in a Java application. 

Explore Java Courses

Scanner class  

The Java.util package’s Scanner class is used to read input data from a variety of sources, including input streams, users, files, etc. 

Java User Input 

In the java.util package is the Scanner class, which gathers user input. By creating an object of the class and using it, you can use any of the several methods given in the Scanner class documentation. The nextLine() method, which is used to read strings, will be utilized in our example:

Also read: The Power of Enum Types in Java

Input Types 

Below are some of the input types we use with the Scanner class. 

Method  Description 
nextBoolean()  Reads a user-provided boolean value. 
nextByte()  Reads a user-supplied byte value. 
nextDouble()   A double value is read from the user. 
nextFloat()  Reads a user-supplied float value. 
nextInt()   Reads a user-supplied int value. 
nextLine()  Reads a value for a String from the user. 
nextLong()  A lengthy value is read from the user. 
nextShort()  Reads a brief value supplied by the user. 

Take this as a case study 

Example 1: Using a Scanner, read a Line of Text 

2023_02_image-176.jpg

Output: 

2023_02_image-134.jpg

Note the line in the example above that says 

2023_02_image-134.jpg

Here, we’ve made an input-named Scanner object. 

To accept input from the standard input, use the System.in argument. It functions exactly like accepting keyboard input. 

Then, we read a line of text from the user using the nextLine() method of the Scanner class. 

Now that you know a bit about scanners, let’s investigate them more thoroughly. 

A Brief Introduction of HashMap in Java
A Brief Introduction of HashMap in Java
HashMap in Java is a class that implements a map interface in the collection framework. In this article, we will briefly cover how to declare and define HashMap in Java...read more
Thread in Java
Thread in Java
Threads in Java allow for concurrent execution, improving the performance and responsiveness of programs. Learn how to create and use threads effectively.
Synchronizing Threads in Java: A Practical Guide
Synchronizing Threads in Java: A Practical Guide
Synchronization in java is used to remove the data inconsistency when multiple threads access the same object or resource simultaneously. In this article, we will briefly discuss different types of...read more

Working of Java Scanner 

The Scanner class reads a line in its entirety and breaks it up into tokens. Small components known as tokens are meaningful to the Java compiler. For instance, 

Consider the following input string: 

2023_02_image-141.jpg

The scanner object will read the entire line in this instance and separate the string into the tokens “Apple,” “costs,” and “100.” After that, the object loops through each token and reads it using one of its various methods. 

Also read: While Loop in Java | Java While Loop Example

Example 2: Scanner Object creation in Java 

Here is how to construct Scanner objects when we import the package. 

2023_02_image-140.jpg

Here, we’ve made Scanner class objects that will read data from files, strings, and input streams, respectively. 

Example 3: Java Scanner nextInt() 

2023_02_image-139.jpg

Output

2023_02_image-138.jpg

The nextInt() method was used to read an integer number in the example above. 

Example 4: Java Scanner next() 

2023_02_image-137.jpg

Output: 

2023_02_image-142.jpg

In the sample above, we read a string from the user using the next() method. 

You can find the complete name here. The next() method, however, just reads the first name. 

This allows the next() method to read input up to the whitespace character. The string is returned when the whitespace is encountered (excluding the whitespace).

Example 5: Java Scanner nextLine() 

2023_02_image-136.jpg

Output

2023_02_image-135.jpg

We have read a string from the user using the nextLine() method. 

The nextLine() method, in contrast to next(), reads the entire input line, including spaces. When a following line character is encountered, the method is stopped. 

Conclusion: 

The Scanner is mostly which we use to parse user input into simple data types like int, double, or the default String. A utility class creates tokens from parsed input using regular expressions. 

Contributed By: Sangeetha Bandari

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio