Scanner issue when using nextLine after nextXXX

Javajava.util.scanner

Java Problem Overview


I've faced an issue when I'm trying to get the user input using Scanner:

import java.util.Scanner;

public class Main
{
	public static Scanner input = new Scanner(System.in);
	public static void main(String[] args)
	{
		System.out.print("Insert a number: ");
		int number = input.nextInt();
		System.out.print("Text1: ");
		String text1 = input.nextLine();
		System.out.print("Text2: ");
		String text2 = input.nextLine();
	}
}

Output:

Insert a number: 55
Text1: Text2: Hi there!

As you can see, the program skipped String text1 = input.nextLine();. What is the problem here? and how to solve this issue?

Java Solutions


Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEng.FouadView Question on Stackoverflow