Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组标价_Java_Arrays_Search_Methods_Double - Fatal编程技术网

Java 数组标价

Java 数组标价,java,arrays,search,methods,double,Java,Arrays,Search,Methods,Double,我正在尝试制作一个价格低于给定数字的书籍的数组列表 我一直收到以下编译器错误: JAmos_Chapter09_exercise_94Arrays.java:86: error: double cannot be dereferenced if ( ( currentJAmos_Chapter09_exercise_94.getPrice( ) ).indexOf( searchString ) != -1 )

我正在尝试制作一个价格低于给定数字的书籍的数组列表 我一直收到以下编译器错误:

   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;
 }
总的来说,我想知道我如何摆脱

无法取消引用double错误

因为我需要搜索双精度而不是字符串

很抱歉,这太长了。

那么,如果
getPrice()
返回一个
double
,为什么要对
double
值执行
indexOf()
?它只是一个数字,你希望它能给你什么索引
indexOf()
用于有序的项目集合

我想知道,如果您处理的是数字,为什么首先需要一个
String searchString
参数。为什么参数a
的价格不是双倍的

如果必须是字符串,则首先通过
double.valueOf()
将其转换为double,然后使用
==
将其与
getPrice()
进行比较。不要使用
.indexOf()
来查找价格是否低于某个值
.indexOf(s)
在另一个字符串中查找字符串第一个实例的起始索引


您正在寻找小于比较运算符:
双精度上没有indexOf函数。您可以使用compareTo并将搜索字符串转换为双精度。但我不确定你想做什么。你不能直接比较double和string。多么糟糕的类名啊。为什么不看书呢?太离谱了,谢谢。我认为这有帮助。我现在想知道maxPrice双精度符号是否会出现在我的类文件中,因为编译器找不到它。@user1848419在本地级别将
maxPrice
声明为双精度符号(正如Baptistewich编辑代码以显示的那样)是个好主意。非常好。谢谢飞越者!我想提醒您,如果您认为我的答案解决了您的问题,请单击“接受答案”复选标记:
 /** 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 );
 }
}
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; 
}