Monday, August 29, 2022

Fix Scanner.nextLine() Skipping Input After Another next Methods

In this article we’ll see why do we have the issue of Scanner.nextLine() skipping the input if used just after any other next method of the java.util.Scanner class and how to fix this problem.

What is the problem

If you are here to look for a solution to this problem you might already have encountered a similar scenario. Here is a program where next() method of the Scanner class is used to get a word as input, then nextLine() is used, then nextInt() and again nextLine() is used.

import java.util.Scanner;

public class ScannerDemo {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a word: ");
		String s = sc.next();
		
		System.out.println("Enter name: ");
		String name = sc.nextLine();
		
		System.out.println("Enter a number: ");
		int i = sc.nextInt();
		
		System.out.println("Enter address: ");
		String addr = sc.nextLine();
		
		System.out.println("Entered word is- " + s);
		System.out.println("Entered name is- " + name);
		System.out.println("Entered number is- " + i);
		System.out.println("Entered address is- " + addr);
	}

}

Output

Enter a word: 
test
Enter name: 
Enter a number: 
1
Enter address: 
Entered word is- test
Entered name is- 
Entered number is- 1
Entered address is- 

As you can see both nextLine() methods are skipped here.

What is the reason of skipping

When you use any of the next method be it next(), nextInt() etc. The input is read till the content ends without reading the newline character which is there because of pressing "enter".

If there is a nextLine() method just after any of the other next methods of the Scanner class, it ends up reading just the newline character (‘\n’) left by the previous next method usage.

As per the documentation of the nextLine() method in Scanner.

Advances this scanner past the current line and returns the input that was skipped.

In the scenario of using it after any other next method it advances past the left over newline character and returns just that input. So it seems, as if nextLine() is skipped.

How to fix this issue

Simple solution is to use an extra nextLine() method in such scenario to consume the left over new line character ('\n') so that the following nextLine() can take the actual input from the user. If we do that to the program used as example previously.

public class ScannerDemo {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a word: ");
		String s = sc.next();
		// extra nextLine
		sc.nextLine();
		System.out.println("Enter name: ");
		String name = sc.nextLine();
		
		System.out.println("Enter a number: ");
		int i = sc.nextInt();
		
		sc.nextLine();
		System.out.println("Enter address: ");
		String addr = sc.nextLine();
		
		System.out.println("Entered word is- " + s);
		System.out.println("Entered name is- " + name);
		System.out.println("Entered number is- " + i);
		System.out.println("Entered address is- " + addr);
	}
}

Output

Enter a word: 
test
Enter name: 
Ramesh Jaiswal
Enter a number: 
10
Enter address: 
1 VIP Road, New Delhi
Entered word is- test
Entered name is- Ramesh Jaiswal
Entered number is- 10
Entered address is- 1 VIP Road, New Delhi

As you can see with the inclusion of two extra sc.nextLine() methods problem of Scanner.nextLine() method skipping the input is solved.

That's all for this topic Fix Scanner.nextLine() Skipping Input After Another next Methods. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. getPath(), getCanonicalPath() and getAbsolutePath() Methods in Java
  2. Buffered Streams in Java IO
  3. How to Read Input From Console in Java
  4. Read File Asynchronously Java Program
  5. Java Program to Delete File And Directory Recursively

You may also like-

  1. Difference Between StackOverflowError and OutOfMemoryError in Java
  2. How to Get The Inserted ID (Generated ID) in JDBC
  3. Java Concurrency Interview Questions And Answers
  4. Static Synchronization in Java Multi-Threading
  5. Spring MVC Generate Response as JSON Example
  6. registerShutdownHook() Method in Spring Framework
  7. Global Keyword in Python With Examples
  8. Angular One-Way Data Binding Using String Interpolation

No comments:

Post a Comment