-1
Scanner keyboard = new Scanner(System.in);
//prompt the user input for a number
int a  = keyboard.nextInt();
//prompt the user input for a string
String str = keyboard.nextLine();

Get Input for String

Pshemo
  • 113,402
  • 22
  • 170
  • 242
  • There is likely still a new line character in the input stream after `nextInt`, which means it's just skipping straight over the `nextLine` – MadProgrammer Nov 27 '15 at 01:19

1 Answers1

0

The .nextLine() is getting the '\n' character trailing the integer. Fix this by adding keyboard.nextLine() after the .nextInt(). As follows:

Scanner keyboard = new Scanner(System.in);
// prompt the user input for a number
int a  = keyboard.nextInt();
// prompt the user input for a string
keyboard.nextLine(); // This captures the '\n' character trailing the integer
String str = keyboard.nextLine();
Bimde
  • 632
  • 8
  • 19