2012-11-25 23 views
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部分得到了雙從該類文件:

/** 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 cannot be dereferenced error

,因爲我需要尋找一個雙,而不是一個字符串。

對不起,如果這很長。

+0

雙倍沒有indexOf函數。您可以使用compareTo並將您的searchString轉換爲double。但我不確定你想要做什麼。你不能直接比較double和string。 –

+0

什麼是一個糟糕的類名。爲什麼不預訂?完全離開基地。 – duffymo

回答

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是不是一個很好的格式化雙,我建議寫一個以searchString轉換爲雙例程。

編輯:如果這不清楚,請在評論中詢問我...

+0

謝謝。我認爲這有助於。我現在想知道maxPrice的雙重符號是否在我的類文件中,因爲編譯器找不到它。 – user1848419

+0

我編輯了聲明maxPrice的代碼。 –

+0

@BaptisteWicht謝謝! – apnorton

0

那麼,如果getPrice()返回一個double,爲什麼你在double上的值爲indexOf()?這只是一個數字,你期望它給你什麼指數? indexOf()用於有序的項目集合。

我想知道,爲什麼您首先期待String searchString參數,如果您正在處理數字。爲什麼不是參數double price也?

如果它必須是一個字符串,那麼首先通過Double.valueOf()將其轉換爲雙倍值,然後使用==將其與getPrice()進行比較。

+0

問題和投機應該添加爲評論... – Dennis