You have what is called an error at a time . One of the basics of many languages ββis that they are zero-based when it comes to indexing. You have half the rights, you have one error (actually two), and instead of fixing this error, you only fixed a symptom ....
Disabled by one error
Error in your for-loop:
for(int index =0; index <= numElements; index++)
If you loop one time too much ... you should use < instead of <= in the test state. This way you will quote numElements times.
Instead of fixing it, you made the 1-element of the queue too large, so you should change:
ArrayBndQueue queue = new ArrayBndQueue<>(numElements + 1);
:
ArrayBndQueue queue = new ArrayBndQueue<>(numElements);
This should parse the extra loop, and you will still have room for the values.
Scanner control error
Scanner.nextInt() only pulls the int value from the scanner, and does not complete a new line / carriage return, so when you call nextLine() in its loop, it clears the line already in the scanner, instead of waiting for the input.
You need to clear the line from the scanner before proceeding after calling nextInt() :
numElements = keyboard.nextInt(); keyboard.nextLine();
This should clear your scanner for next input.
From the documentation :
nextInt () - scans the next input token as int. This method will throw an InputMismatchException if the next token cannot be converted to a valid int value, as described below. If the transfer is successful, the scanner advances past the input.
"moves past the input that matches" means before returning a new line / carriage.