Scanner class nextLine() issue resolved in JAVA
Sai Laasya Vabilisetty
Posted on September 18, 2020
Hello everyone!π
This article highlights the basic theme of the scanner and a common error while using nextLine() method. So why late, let's dive into the topic!π€Ώ
CONTENTS
What is Scanner class?
The scanner class belongs to java.util package. It is used for obtaining input for data types like int, char, double (all primitive types), string, used to read a file by passing file object. It can also be used for wrapper class objects.
We can access the methods of scanner class by a scanner object. While creating an object we pass System.in
as a parameter. This tells the java compiler that the input will be provided through standard input (keyboard).
//creating a scanner object
Scanner scan=new Scanner(System.in);
Some basic methods in Scanner class
Some of the basic methods you need to know while using scanner class are
- nextInt(): It scans the next token of the input as an int value.
int a=scan.nextInt();
- nextDouble(): It scans the next token of the input as double value.
double a=scan.nextDouble();
- nextLine(): It scans from the current position until it finds a line seperator.
String s=scan.nextLine();
This will be explained more clearly in the next section. There are various other methods in Scanner class, you can check it here.
Why nextLine() method skips input?
So, to understand this clearly, let's start with an example.
public class ScannerExample {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter account number");
int account_num=sc.nextInt();
System.out.println("Enter account holder's name");
String name=sc.nextLine();
System.out.println("Enter the amount to deposit");
double amount=sc.nextInt();
System.out.print("Account number: "+account_num);
System.out.print("Name: "+name);
System.out.print("Amount: "+amount);
}
}
Here, we are taking input for 3 variables account_num
, name
, amount
using respective scanner methods, which we have learnt earlier. Let's check the output now.
Expected Output:
Account number: 154623
Name: john doe
Amount: 4000.0
Output we get:
Account number: 154623
Name:
Amount: 4000.0
Did you observe the difference?π€The input we give using nextLine()
for name
is skipped and continued with the next one.
Why did this happen?
So, when we used nextInt()
to get account_num
it scans till the end of input 154623 and the cursor remains there after reading the number like this
154623π //cursor position after reading input
- >Now, we know
nextLine()
is used to take a string as an input which there by accepts spaces too.
So, when we use nextLine()
, it scans from where the cursor is and takes empty space as input. That's the reason why we got an empty line for name
.
How to resolve this?
- We can just use a
nextLine()
method right after taking int as input, so it takes the empty space away and allows us to give input forname
. The code looks something like this:
System.out.println("Enter account number");
int account_num=sc.nextInt();
sc.nextLine() //this takes away the empty space as input
System.out.println("Enter account holder's name");
String name=sc.nextLine();
- We can also scan the complete line for integer using
nextLine()
and convert it to int usingInteger.parseInt()
, sonextLine()
reads till end of line andparseInt()
converts data into int.
System.out.println("Enter account number");
int account_num=Integer.parseInt(sc.nextLine());
System.out.println("Enter account holder's name");
String name=sc.nextLine();
So, whenever you are using nextLine()
after using any methods to get input like int, double, float etc, make sure that you apply these approaches.
If you are reading this line, you have reached the end of the article and you have learnt a new thing today.π
Happy Java'ing!π
Posted on September 18, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.