I’m trying to make an ArrayList of Books whose price is less than a given number and
I keep getting this compiler error:
JAmos_Chapter09_exercise_94Arrays.java:86: error: double cannot be dereferenced
if ( ( currentJAmos_Chapter09_exercise_94.getPrice( ) ).indexOf( searchString ) != -1 )
^
1 error
after compiling this method in my array list file’s code:
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;
}
the getPrice part of my method code gets a double from this class file:
/** 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 );
}
}
Overall, I’m wondering how I get rid of the
double cannot be dereferenced error
since I need to search for a double rather than a String.
Sorry if this was long.
Don’t use
.indexOf()to find if the price is less than a certain value..indexOf(s)finds the starting index of the first instance of a string in another string.You are looking for the less-than comparison operator:
<.Change your logic to:
If your
searchStringis not a nicely formatted double, I suggest writing a routine to convertsearchStringinto a double.EDIT: If this wasn’t clear, please ask me about it in the comments…