According to the doc:
Throws:
InputMismatchException – if the next token does not match the Integer regular expression, or is out of range
So i thing you could do this instead:
int counter = 0;
while (counter < numbers.length) {
if (sc.hasNextInt()) {
numbers[counter++] = sc.nextInt();
} else {
if (sc.hasNext("finish")) {
sc.close();
break;
} else {
System.out.println("It's neither number nor 'finish'.");
sc.next();
}
}
}
for (int x : numbers) {
if (x > largestNumber) {
largestNumber = x;
}
}
System.out.println("The largest number is: " + largestNumber);
hope that helps
8
solved Why doesn’t the following program work? [closed]