我試圖讓書籍,其價格低於給定數量和 我不斷收到此編譯器錯誤的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
,因爲我需要尋找一個雙,而不是一個字符串。
對不起,如果這很長。
雙倍沒有indexOf函數。您可以使用compareTo並將您的searchString轉換爲double。但我不確定你想要做什麼。你不能直接比較double和string。 –
什麼是一個糟糕的類名。爲什麼不預訂?完全離開基地。 – duffymo