我正在写一个文件阅读器,这种想法是让用户输入一个数字,表示从文本文件中的行号循环。包含此号码的变量类型为int
。但是,当用户输入一个String
相反,Java的抛出InputMismatchException
例外,我要的是有catch
条款,在那里我会循环,直到用户输入一个有效的值,即一个int
在一个循环。骨架看起来是这样的:爪哇 - 在try-catch块
public void _____ throws IOException {
try {
// Prompting user for line number
// Getting number from keyboard
// Do something with number
} catch (InputMismatchException e) {
// I want to loop until the user enters a valid input
// When the above step is achieved, I am invoking another method here
}
}
我的问题是,什么是可以做验证一些可能的技术? 谢谢。
谢谢。这是有效的,但我必须在'catch'子句中的提示符之后添加一个额外的String ___ = __。nextLine()。 –