Using Scanner object to read user input in Java

Scanner class in Java is a class that helps us to read data from many different sources such as user input when running the application, files. In this tutorial, I will show you how to read the information that the user enters using this Scanner class!

First, we will initialize the object of this Scanner class.

The Scanner class has many different constructors. To receive data entered by the user, please initialize this object with the constructor of a standard input stream,

The data that the user enters can be a string or a number, so the Scanner class also has corresponding methods to support this, as follows:

  • nextLine()
  • next()
  • nextInt()
  • nextLong()
  • nextFloat()
  • nextDouble()
  • nextBoolean()
  • nextByte()
  • nextShort()
  • nextBigInteger()
  • nextBigDecimal()

If the data is an integer, you can use the nextInt() or nextByte() or nextShort() or nextLong() or nextBigInteger() methods to read this data. Depending on the information entered, you can use appropriate methods so that the program can read that value.

For example, to read a person’s age information, you can use the nextByte() method (byte data type with a range of values from -128 to 127) as follows:

The output when running the above example is as follows:

To read decimal data type, you can use the methods nextDouble(), nextFloat(), nextBigDecimal(). Examples are as follows:


With the input data type as boolean, you can use the nextBoolean() method, for example as follows:


For the input data type is a string, the Scanner class supports us with 2 methods, next() and nextLine(). The difference between these two methods is: the next() method will read the user’s input word by word, and nextLine() will read the entire string on the same line of the user.

For example, if you use the next() method to read the following information:

then when running this example, if you enter “Khanh”, the result will print the words “Result: Khanh”:

But if you enter “Huong Dan Java”, the result is only “Huong”:

That’s because the next() method only reads word by word from the data you enter. To read all the words “Huong Dan Java” above, you need to add code to read each of these words. For example, I will add the following code:


If you use the nextLine() method, you just need to write the following code to read the text “Huong Dan Java” entered by the user:

The nextLine() method will take a carriage return or enter to mark the end of a line and return the data of that line.


A note that you need to know: only the nextLine() method will read the content that the user enters including the newline or enter to return the result. As for the rest of the next…() methods, only the user input value is returned, the newline or enter characters remain in the input buffer. So if you, for example, use the nextLine() method after calling using the nextInt() method:

when running the above example, after entering the age, you will see that the program will end without letting us enter the name:

The reason for this is that after we enter the age, the next nextLine() method will read the newline or enter character that the nextInt() method does not read, to assign to the variable name above.

To solve this problem, you can call another method nextLine() after the above nextInt() method to read the newline character or enter in the input buffer, then call the other nextLine() method to enter name information. Examples are as follows:

The result when you run this example again is as follows:

Another way to solve this problem is that you always use the nextLine() method to read information entered by the user. Then parse this information to the data type we want. Examples are as follows:

The result is still the same:

The Scanner class also has has…() methods that allow us to check if there is any input from the user? Only then read the content. Examples are as follows:


Add Comment