Помогите пожалуйста с Java-программой
Помогите пожалуйста с программой(( Дали на учёбе задание:
Напишите программу, которая считывает строки, вводимые пользователем, ввод закончить, если введена строка "quit". Данные вводить с помощью класса Scanner, для исключения неправильного ввода, использовать механизм обработки исключений try/catch
Исключением же является:
public String nextLine() Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line. Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.
Returns: the line that was skipped Throws: NoSuchElementException - if no line was found
import java.util.Scanner;
import java.util.NoSuchElementException;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
while (true) {
System.out.println("Введите строку (quit для завершения):");
String input = null;
try {
input = scanner.nextLine();
} catch (NoSuchElementException e) {
System.out.println("Ошибка ввода: " + e.getMessage());
break;
}
if (input.equals("quit")) {
break;
}
}
} catch (Exception e) {
System.out.println("Ошибка ввода: " + e.getMessage());
} finally {
scanner.close();
}
}
}
По сути исключение должно работать если в поле ввода нажать Ctrl+D, ввести пустую строку или закрыть консоль. Так вот, вопрос в том, как ввести пустую строку? При нажатии на enter во время работы программы не вылезает сообщение "Ошибка ввода:"