2

このプログラムを実行すると、エラー Exception in thread "main" java.lang.NumberFormatException: empty String が表示されます。何らかの方法で例外をスローすることでこれを機能させることは可能ですか、または try catch ステートメントを使用する必要がありますか?

private double[] scores3 = new double[5]; 
Scanner keyboard = new Scanner(System.in);

public void enterData() 
{
   double score;
   double numberMin = 0;
   double numberMax = 100;



do{
    System.out.println("Enter the student's third test score");

      while (!keyboard.hasNextDouble()) {

      keyboard.next();
      }
       score3 = keyboard.nextDouble();


        }
         while  (score3 <= numberMin || score3 >= numberMax);
         double score3 = Double.parseDouble(keyboard.nextLine());
4

2 に答える 2

1

文字列を数値に解析する前に..文字列を「」と比較するか、isEmpty()関数を使用して空の文字列かどうかを確認してください。

例:

if (myString.isEmpty()) {
    //Do Nothing
} else {
    //Code to perform calculations
}

また

if (!myString.isEmpty()) {
    //Code to perform calculations
}
于 2013-01-29T19:15:42.097 に答える
0

他の場所でエラーをスローできるかどうかという質問に対して、宣言することでそれを行うことができます

public void enterData() throws NumberFormatException

ただし、while ステートメントを繰り返し処理したい場合は、try catch ブロックを使用することをお勧めします。

于 2013-01-29T19:14:38.187 に答える