So I am trying to let the user input a message and an integer. The integer determines how many times the message will be printed to the screen. I am using a for loop for the repetition.
My problem now is that when I let the user input the message first and then the integer everything works fine, but when I do it the other way around, it does not let me enter the message after entering the integer. After typing in the integer and pressing enter, it display the prompt for entering the message but then immediately exits the program.
What causes this behaviour?
// This application lets the user enter an integer and a message.
// The message is printed as many times as the integer that was specified.
package en.hkr;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x;
String message;
// Does not work the other way around?
System.out.print("Enter a message: ");
message = input.nextLine();
System.out.print("Enter an integer: ");
x = input.nextInt();
// for loop
for(int i = 0; i < x; i++) {
System.out.println(message);
}
}
}