Scanner class Methods in Java

Scanner Class 

We have worked with Scanner class in all of our programs. This post is specially to understand some commonly used and important methods of Scanner class. I will also elaborate an error which occurs due to nextLine() method.

Scanner class is the class which is used to read input. It resides in java.util.Scanner package. Whenever we want to get input from user, we can use this class. 

How to read input from user using Scanner?

Using Scanner class is very easy task. We only need to create its object and use that object to call its methods to read the data user is going to enter. Lets do this by following some easy steps:

1. Import java.util.Scanner in your class by writing:

                                 import java.util.Scanner;

2. Create a object of Scanner class using new operator as shown below:

                                Scanner scan=new Scanner(System.in);

3. Now , you can use its object to call its methods corresponding to datatype of variable. For example, if you want to read a variable name "a" of int type then use nextInt() method as shown below:

                                    a=scan.nextInt():

4. Close the Scanner object. Closing scanner object prevents memory leak. Close is by using following code:

                                   scan.close();

Scanner Methods

Scanner class has methods which read the primitive data type. Depending on which datatype we are using we have to use the corresponding scanner method. There are mainly five datatypes that are commonly used by programmers that are int, float, double, String, long, char etc. We will learn the methods used for these datatypes.

Methods of Scanner Class:

0. next()

1. nextInt()

2. nextFloat()

3. nextDouble()

4. nextLong()

5. next().charAt()

6. nextLine()

Lets learn about each method in detail.

0.) next()

This method is the main method of Scanner class . This is used to get the next token. The next() method find and returns the next token in the program. 

There are three types of this method:

a. next() - It has no parameters.

b. next(String pattern ) - It  contains string as a parameter. It matches the next token of string with the pattern passed in argument.

c. next(Pattern ptr)-  It returns the next token if it matches with the specified pattern.

1.) nextInt()

This method is used to scan or read the integer value inputted by user .If you want to scan the value like numbers without decimal position called as 'int' datatype then this method is used. 

Example: To get the age of any person in a program we can use following code:- 

          int age;

          Scanner scan=new Scanner(System.in);

          System.out.println("Enter your Age: ");

          age=scan.nextInt(); 

In the above example, we use System.out.println(); to give information to user that he is supposed to enter the age.

2.) nextFloat()

If we want to read a variable of float datatype we use nextFloat() method. These datatype contains the values which have decimal point.

Example: 

                 float height;

                  height=scan.nextFloat();

3.) nextDouble()

If we want to read a variable of double datatype we use nextDouble() method. These datatype contains the values which have decimal points. Double datatype contains the decimal values as in the float datatype. The only difference is that it can contain numbers with the double precision than float datatype.

Example:

                  double weight;

                  weight= scan.nextDouble();


4.) nextLong()

This method is used to scan or read the integer value which are above the range of normal 'int' datatype. If you want to scan the value like numbers without decimal position then this method is used. This datatype is known as 'long'.

Example: 

          long age; 

          age=scan.nexLong(); 

5.) next().charAt(int parameter)

There is no direct method to read 'char' datatype. Therefore, next() method is used along with 'charAt()'  method. This charAt method contains the index of character as a parameter. Usually we pass 0 as index. It we have created an char array using characters . Then we can use this method to read the characters of char array.

Example:

               char ch;

               ch= next().charAt();

6.) nextLine()

This method is used to read the 'String' datatype values. If we want to read a String entered by user than we use this method. 

Example:

                 String str;

                 str= nextLine();

Bug:

There is a limitation of this method. The con of nextLine() method is that if we try to read a string after nextInt() or nextDouble() or any nextFoo() method then nextLine() method doesnot read the string entered. Because it read the next line that becomes empty after caling in next method and it doesnot give time to user to input a string. 

For example : Try the following code:


By trying above code , you'll see an bug in the code that it does not let you to input or read the string value. You can see the output as below:

Fix: 

The best fix to this bug is that we should invoke nextLine() method to jump to next line to read the string value.

Example: In the previous program, we have seen that the nextLine() is not able to read Strings..So, now we can invoke empty nextLine() method as shown below:

Output:

The problem is fixed by writing an one line code as in above example at line 15.


The reason behind writing this post was this bug that I found and thought many people will be suffering from it. If you like the post leave a comment. Thanks for reading. Keep Coding!



Comments