2
    Scanner kb = new Scanner(System.in);
    System.out.println("Inserting L");
    int L = kb.nextInt();   
    System.out.println("Inserting N");
    int N = kb.nextInt();
    System.out.println("Inserting x");
    String x = kb.nextLine();
    System.out.println(x);
    System.out.println("You inputed L,N,x");

Why is there no prompt for nextLine() in this code?

Kailua Bum
  • 1,250
  • 5
  • 25
  • 39
Smile.Hunter
  • 217
  • 4
  • 12

2 Answers2

3

Use:

String x = kb.next();

instead of kb.nextLine().

This is because nextInt() reads just the number, not the end of line or anything after the number. When you call nextLine() it reads the remainder of the same line and does not wait for input.

Ideally you should call kb.nextLine() the line after your code for nextInt() to ignore the rest of the line.

Community
  • 1
  • 1
dogbane
  • 242,394
  • 72
  • 372
  • 395
0
Scanner kb = new Scanner(System.in);
    System.out.println("Inserting L");
    int L = kb.nextInt();   
    System.out.println("Inserting N");
    int N = kb.nextInt();
    System.out.println("Inserting x");
    String x = kb.next();
    System.out.println(x);
    System.out.println("You inputed L,N,x");

try this one...

Dmitri Gudkov
  • 1,935
  • 14
  • 15