You're calling Scanner#nextLine
twice - once in the while
loop expression and again in the body of the loop. You can just assign input
from the while
loop expression. In addition you can use Scanner#hasNextLine
to defend against NoSuchElementException
occurring:
while (userInput.hasNextLine() &&
!(input = userInput.nextLine()).isEmpty()) {
System.out.println("Course accepted: " + input);
counter++;
}