NoSuchElementException with Java.Util.Scanner

前端 未结 7 704
感动是毒
感动是毒 2020-11-22 14:38

I am very new to Java but am working through the book Java: How to program (9th ed.) and have reached an example where for the life of me I cannot figure out what the proble

7条回答
  •  被撕碎了的回忆
    2020-11-22 15:08

    Integer#nextInt throws NoSuchElementException - if input is exhausted

    You should check if there is a next line with Integer#hasNextLine

    if(sc.hasNextLine()){
        number1=sc.nextInt();
    }
    

提交回复
热议问题