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
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
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();