gpt4 book ai didi

java - 阵列标价

转载 作者:行者123 更新时间:2023-12-01 15:01:42 25 4
gpt4 key购买 nike

我正在尝试创建一个价格低于给定数字的书籍数组列表,并且我不断收到此编译器错误:

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

因为我需要搜索 double 而不是字符串。

抱歉,如果这篇文章太长了。

最佳答案

不要使用.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变成双。

编辑:如果不清楚,请在评论中询问我......

关于java - 阵列标价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13547946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com