0

価格が指定された数値よりも低い書籍の ArrayList を作成しようとしていますが、次のコンパイラ エラーが発生し続けます。

   JAmos_Chapter09_exercise_94Arrays.java:86: error: double cannot be dereferenced
   if ( ( currentJAmos_Chapter09_exercise_94.getPrice( ) ).indexOf( searchString ) != -1 )
                                                               ^
1 error

私の配列リストファイルのコードでこのメソッドをコンパイルした後:

 public ArrayList<JAmos_Chapter09_exercise_94> searchForPrice( String searchString )
 {
  ArrayList<JAmos_Chapter09_exercise_94> searchResult = new ArrayList<JAmos_Chapter09_exercise_94>( );
  for ( JAmos_Chapter09_exercise_94 currentJAmos_Chapter09_exercise_94 : library )
  {
   if ( ( currentJAmos_Chapter09_exercise_94.getPrice( ) ).indexOf( searchString ) != -1 )
        searchResult.add( currentJAmos_Chapter09_exercise_94 );
  }
  searchResult.trimToSize( );
  return searchResult;
 }

メソッド コードの getPrice 部分は、このクラス ファイルから double を取得します。

 /** default constructor
 */
 public JAmos_Chapter09_exercise_94( )
 {
  title = "";
  author = "";
  price  = 0.0;
 }

 /** overloaded constructor
 *  @param newTitle   the value to assign to title
 *  @param newAuthor  the value to assign to author
 *  @param newPrice   the value to assign to price
 */
 public JustinAmos_Chapter09_exercise_94( String newTitle, String newAuthor, double newPrice )
 {
  title = newTitle;
  author = newAuthor;
  price  = newPrice;
 }

 /** getTitle method
 *   @return the title
 */
 public String getTitle( )
 {
  return title;
 }

 /** getAuthor method
 *   @return the author
 */
 public String getAuthor( )
 {
  return author;
 }

 /** getPrice method
 *   @return the price
 */
 public double getPrice( )
 {
  return price;
 }

 /** toString
 * @return title, author, and price
 */
 public String toString( )
 {
  return ( "title: " + title + "\t"
           + "author: " + author + "\t"
           + "price: " + price );
 }
}

全体的に、私はどのように取り除くのだろうかと思っています

double は逆参照できませんエラー

String ではなく double を検索する必要があるためです。

これが長い場合は申し訳ありません。

4

2 に答える 2

1

.indexOf()価格が特定の値よりも低いかどうかを調べるために 使用しないでください。.indexOf(s)別の文字列内の文字列の最初のインスタンスの開始インデックスを見つけます。

小なり比較演算子を探しています: <.

ロジックを次のように変更します。

public ArrayList<JAmos_Chapter09_exercise_94> searchForPrice( String searchString ) {  
  ArrayList<JAmos_Chapter09_exercise_94> searchResult = new ArrayList<JAmos_Chapter09_exercise_94>( ); 
  //Converts the search string price into a double price.
  double maxPrice = Double.parseDouble(searchString);
  for ( JAmos_Chapter09_exercise_94 currentJAmos_Chapter09_exercise_94 : library ) {
    //If itemPrice < maxPrice, add it to the list.
    if ( currentJAmos_Chapter09_exercise_94.getPrice( ) < maxPrice)
      searchResult.add( currentJAmos_Chapter09_exercise_94 );
  } 
  searchResult.trimToSize( ); 
  return searchResult; 
}

searchString適切にフォーマットされた double でない場合はsearchString、double に変換するルーチンを作成することをお勧めします。

編集:これが明確でない場合は、コメントで質問してください...

于 2012-11-25T03:27:35.390 に答える
0

getPrice()では、 を返す場合double、なぜindexOf()そのdouble値を使用するのでしょうか? これは単なる数字ですが、どのような指標が得られると思いますか? indexOf()注文されたアイテムのコレクションで使用されます。

String searchString数値を扱っているのに、そもそもなぜパラメーターを期待しているのだろうか。パラメータが a ではないのはなぜdouble priceですか?

文字列でなければならない場合は、最初に double に変換してから、 usingDouble.valueOf()と比較します。getPrice()==

于 2012-11-25T03:26:38.477 に答える