59

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?

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417

0 Answers0