NoSuchElementException with Java.Util.Scanner

前端 未结 7 659
感动是毒
感动是毒 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 14:59

    NoSuchElementException Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.

    http://docs.oracle.com/javase/7/docs/api/java/util/NoSuchElementException.html

    How about this :

    if(input.hasNextInt() )
         number1 = input.nextInt(); // if there is another number  
    else 
         number1 = 0; // nothing added in the input 
    

提交回复
热议问题