Understanding Java Scanner Class
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!
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.
Best-suited Java courses for you
Learn Java with these high-rated online courses
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
Output:
Note the line in the example above that says
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.
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:
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.
Here, we’ve made Scanner class objects that will read data from files, strings, and input streams, respectively.
Example 3: Java Scanner nextInt()
Output:
The nextInt() method was used to read an integer number in the example above.
Example 4: Java Scanner next()
Output:
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()
Output:
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
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