Stuck at “Exception in thread ”main" java.util.NoSuchElementException

后端 未结 2 1852
有刺的猬
有刺的猬 2021-01-25 04:45

I wrote a program with a separate class but I keep getting the same error right after the user inputs the three sides.

The main code is:

package interac         


        
2条回答
  •  太阳男子
    2021-01-25 05:00

    Scanner#nextDouble throws NoSuchElementException

    if the input is exhausted

    The way do avoid it, and the correct way to use scanner generally (also for iterators) is to first check if there is such an input available. Try:

    if (scanner.hasNextDouble())
        scanner.nextDouble();
    

    If the call returns false then there is simply no double to read.

提交回复
热议问题