If your Scanner
is declared as System.in
, it will be asking System.in
if there is a next value to be received. The in
class will then wait for the user to input something, then return true.
Reserve hasNext()
for things like files, where the input size is fixed. That way, when Scanner
queries the file, it has a definite hasNext()
.
Instead, I suggest grabbing the next value as a String, then checking if it matches the integer format. If it does, then convert it, otherwise, handle it however you'd like.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…