質問
私は例外ループにしようとしていますが、何らかの理由でそのは私に再び私のスキャナファイルを書き込むためのオプションを与えていません
私はこれを使用している理由ですので、BufferedReaderのを使用する方法がわかりません。任意の手がかり?
ここに私の標準クラスは、私の方法でだ。
package arrayExceptionsWithInput;
import java.util.*;
public class GetThoseNumbersBaby {
int firstInt;
int secondInt;
boolean error;
Scanner yourNumbers = new Scanner(System.in);
public void findNumbers()
{
System.out.println(" please enter your first number");
firstInt = yourNumbers.nextInt();
System.out.println(" pleas enter your second number");
secondInt = yourNumbers.nextInt();
int finalInt = (firstInt+secondInt);
System.out.println("total is: " + finalInt);
}
}
そして、ここでは、ループでimplemetedされている例外を除いて、私のメインクラスです
package arrayExceptionsWithInput;
import java.util.*;
public class InputException
{
public static void main(String[] args)
{
boolean error = false;
GetThoseNumbersBaby zack = new GetThoseNumbersBaby();
{
do {
try
{
zack.findNumbers();
}
catch(InputMismatchException Q)
{
System.out.println(" one of your integers was incorrect, please try again");
Q.printStackTrace();
error = true;
}
} while (error == true);
}
error = false;
}
}
誰がどのようにループこれをする上の任意のアイデアを持っている場合は、は異なり、私はすべての耳だ。
解決 2
私は、メソッドのクラスを取り除くあまりにも決めました そしてちょうどトライ例外エリアにメソッドを入れます。
スキャナ後.nextLineを使用し、それが一定のようです。
これはOKに見えます?
package arrayExceptionsWithInput;
import java.util.*;
public class InputException
{
public static void main(String[] args)
{
boolean error = false;
int firstInt;
int secondInt;
Scanner yourNumbers = new Scanner(System.in);
{
do{
try
{
System.out.println(" please enter your first number");
firstInt = yourNumbers.nextInt();
yourNumbers.nextLine();
System.out.println(" pleas enter your second number");
secondInt = yourNumbers.nextInt();
yourNumbers.nextLine();
int finalInt = (firstInt+secondInt);
System.out.println("total is: " + finalInt);
yourNumbers.nextLine();
}
catch(InputMismatchException Q)
{
Q.printStackTrace();
System.out.println(" one of your integers was incorrect, please try again");
error = true;
yourNumbers.nextLine();
}
}while (error == true);
}error = false;
}
}
他のヒント
に設定し、エラー偽ののアクションの前に。ユーザーが右のそれを取得する場合その方法、あなたは正しい終了条件を持っています。
error = false;
zack.findNumbers();
あなたwhileループ 'エラー' の変数には値の '真' のを持っています。しかし、それは(すなわち時に 'キャッチ' のブロックが実行される)スローされたときにのみのの '真' になります。制御フローは、それに到達しない。
所属していません StackOverflow