DEV Community

Cover image for Scanner class nextLine() issue resolved in JAVA
Sai Laasya Vabilisetty
Sai Laasya Vabilisetty

Posted on • Originally published at laasyasettyblog.hashnode.dev

Scanner class nextLine() issue resolved in JAVA

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 for name. 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 using Integer.parseInt() , so nextLine() reads till end of line and parseInt() 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!😊

Top comments (0)