2016-10-15 17 views
0

私のprint()メソッド内でgetCurrent()メソッドを呼び出すときに、コンソールにエラーメッセージを表示するのが苦労しています。また、私のgetCurrent()メソッドでは、コンパイラは私がダブルを返す必要があると言います。私は戻って二重の問題を理解していない、getCurrent()の呼び出しの周りにブロックラップをキャッチしようとすべきではありません。コンソールに例外エラーメッセージを出力します。

getCurrent方法:

public double getCurrent() throws IllegalStateException{ 
    //check if element 
    try{ 
     if(isCurrent() == true){ 
      return data[cursor]; 
     }else{ 
      throw new IllegalStateException 
      ("No Element"); 
     }//end check   
    }//end try 
    catch(IllegalStateException e){ 
     //print error message to console 
     System.out.println(e.getMessage()); 
    }//end catch   
}//end method 

isCurrent()メソッド:

public boolean isCurrent(){ 
    if(cursor < manyItems){ 
     return true; 
    }else{ 
    return false; 
    } 
}//end method 

プリント()メソッド:

public void print(){ 
    double answer; 
    System.out.println(" Capacity = " + data.length); 
    System.out.println(" Length = " + manyItems); 
    System.out.println(" Current Element = " + getCurrent()); 
    System.out.print("Elements: "); 
    for(int i = 0; i < manyItems; i++){ 
     answer = data[i]; 
     System.out.print(answer + " "); 
    }//end loop 
    System.out.println(" "); 
}//end method 

メイン方法(調整できない)。

DoubleArraySeq x = new DoubleArraySeq(); 
    System.out.println("sequence x is empty"); 
    x.print(); 
    System.out.println("Trying to get anything from x causes an exception\n"); 
    System.out.printf("%5.2f", x.getCurrent()); 

正しい出力:xから何かを取得しようと

系列x = 10

長さ= 0

どの要素

要素空

容量であります例外が発生する

答えて

0
public double getCurrent() throws IllegalStateException{ 
    //check if element 
    try{ 
     if(isCurrent() == true){ 
      return data[cursor]; 
     }else{ 
      throw new IllegalStateException("No Element"); 
     }//end check   
    }//end try 
    catch(IllegalStateException e){ 
     //print error message to console 
     System.out.println(e.getMessage()); 
    }//end catch   
}//end method 

あなたはthrows IllegalStateExceptionをキャッチします。主なごtry{}catch(){}

public double getCurrent() throws IllegalStateException{ 
    //check if element 
    if(isCurrent() == true){ 
     return data[cursor]; 
    }else{ 
     throw new IllegalStateException("No Element"); 
    }//end check  
}//end method 

を削除:

try{ 

    DoubleArraySeq x = new DoubleArraySeq(); 
    System.out.println("sequence x is empty"); 
    x.print(); 
    System.out.println("Trying to get anything from x causes an exception\n"); 
    System.out.printf("%5.2f", x.getCurrent()); 
}catch(IllegalStateException e){ 
    System.err.println("This exception produce because there is no element"); 
} 
+0

は、今私は、Javaからエラーを取得する: – user2018392

+0

今、私は、Javaからエラーを取得する:スレッドの例外「メイン」java.lang.IllegalStateException:いいえ要素代わりに "NO要素" エラーMのDoubleArraySeqDemonstration.main(DoubleArraySeqDemonstration.java:48)で DoubleArraySeq.getCurrentで\t(DoubleArraySeq.java:144) DoubleArraySeq.printで\t(DoubleArraySeq.java:202) \tエッセージがコンソールに印刷されます。 – user2018392

+0

正しいです。あなたは{} catch(){}をメインに使うか、 'getCurrent()'を呼び出すときに答えが必要です。 – user3502626

関連する問題