I am fairly new to Java programming and have been using a for loop in my program. I noticed whenever my program asks for user input, that whenever I press the enter key that is used as the next key for the second iteration of the loop. Is there a way to make this not occur?
Asked
Active
Viewed 53 times
-4
-
Does this answer your question? [How to read input with multiple lines in Java](https://stackoverflow.com/questions/2296685/how-to-read-input-with-multiple-lines-in-java) – funky Feb 19 '21 at 00:44
-
2Welcome to StackOverflow. This question really needs a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) to go with it. Cheers. Also do check out the other answer @funky mentions. – Adam Burke Feb 19 '21 at 00:45
1 Answers
1
Use while loop instead:
while(true){
String input = JOptionPane.showInputDialog(null, "Please enter something.");//take inputs while have not encountered STOP
if(input.equals("STOP")){
System.exit(0);
}
}

Meri Khurshudyan
- 92
- 10