I think you problem may be here
while (input.hasNext()) {
int num = Integer.parseInt(input.next());
if (input.next() == "") {
The second line reads an input, but so does the next line. So when the loop is at the last element, the first line will read it, then the next line will try to read a next element that doesn’t exist, hence NoSuchElementException
.
And don’t compare strings with ==
. Use equals()
or equalsIgnoreCase()
1
solved Programming Challenges 110106 [closed]