Stopping reading inputs when an empty line is entered while entering the input one after another is one of the frequently encountered errors in Java. The solution is to check user inputs.
Java Stop Reading After Empty Line
If we are using for loop to fill the array with user input, it is necessary to rewind the for loop by checking the empty lines entered by the user and only get the validated inputs
String[] array = new String[10]; Scanner scanner = new Scanner(System.in); System.out.println("Please enter 10 inputs"); for (int i=0; i<10; i++) { String input = scanner.nextLine(); if (input.equals("")) { i--; continue; } array[i] = input; } System.out.println(Arrays.toString(array));
Console Output:
Please enter 10 inputs hi hello test
blank empty
line 1 2 3 4 [hi, hello, test, blank, empty, line, 1, 2, 3, 4]