Scanner Class Java

Scanner Class Java

Well, generally when we write some program, the general flow is something like this –

You give some input, the input is processed, and then you receive some output. This is what usually happens. Let’s take the example of adding two numbers. The flow goes like we take two numbers for adding, and then we do the addition operation on the number (This is the processing on the input), and then we show the output (result), to the user.

Taking User Input in Java | Scanner Class

So, just imagine that every user has a different set of inputs. So, are you going to edit those values in the source code every time? Well… no. Here, we make use of user input. In short, we are leaving the input values to the user. Whatever the user enters, we evaluate those values. Not only in java, but in any other programming language, this is followed. Taking user input in java is very easy.

We are going to make use of the Scanner class. The Scanner class has different methods to take different inputs. This means that if you want to take integer input, you use some method, or if you want to take String or boolean or floating-point number as an input, you have separate methods, which we are going to see further.

In java, there are some already written classes that we can use directly use into our program so that we can make different things. To do this, we need to make use of the import statement. So, we import the Scanner class from java.util package. You just have to write this one line in your code so that you can use the Scanner class.

import java.util.Scanner

In the Scanner class, we have a lot of methods for taking different user inputs, some of which we are going to discuss now.

Methods for taking inputs of different types of inputs from the user –

MethodDetails
nextInt()With this method, we can take int-type user input values from the user.
nextFloat()With this method, we can take float-type user input values from the user.
next()With this method, we can take String input(only one word) from the user.
nextLine()With this method, we can take the line of text as user input.
nextBoolean()With this method, we can take boolean type input(true or false) from the user.
nextByte()With this method, we can take byte type input from the user.
nextShort()With this method, we can take short-type input from the user.
nextDouble()With this method, we can take double-type data as input from the user.
hasNextInt()This method returns true if the input by the user is an int type input
hasNext()With this method, we get a true output if there is some input token. (If you feel confused with these methods, don’t worry, just focus on the implementation of these methods. You will get used to them. This applies to all the other methods)
hasNext(String pattern)This method returns a true output if and only if the next token matches the given string pattern. We are going to see the implementation of this method ahead, so don’t worry if you feel confused here.
hasNextByte()This method returns true if the user input is a byte type input.
hasNextBoolean()This method returns true if the user input is of boolean type
hasNextFloat()This method returns true if and only if the input by the user is a float type.
hasNextDouble()This method returns true if and only if the input from the user is of type double.
hasNextShort()This method returns true if and only if the input from the user is of type short.
hasNextLong()This method returns true if and only if the input from the user is of type Long
nextLong()With this method, we can take long type data as input from the user.

Well, above were some of the methods from the Scanner class.