I'm using NetBeans 8.2 on Ubuntu. Is there any way to make s.hasNextDouble() returns false using keyboard input, so that the last line of the code is executed, without changing the code? This code snippet calculates the average of the entered numbers.
double sum = 0;
int n = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter 1 number: ");
while (s.hasNextDouble()) {
double number = s.nextDouble();
sum += number;
n++;
System.out.print("Enter " + (n + 1) + " number: ");
}
System.out.println();
if (n == 0) {
System.out.println("Error!!!");
} else {
System.out.println("Average: " + sum / n);
}