私はファイルリーダーの作成に取り組んでいます。ユーザーがテキストファイルの行番号を表す数値を入力するようにしています。この番号を保持する変数の型はint
です。しかし、ユーザがString
を入力すると、JavaはInputMismatchException
例外をスローし、ユーザが有効値、すなわちint
を入力するまでループするcatch
句にループを設定します。スケルトンは次のようになります:try-catchブロックのJavaループ -
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()を追加する必要がありました。 –