Java 在我的第二个名为下一个类的类中,公共数组列表的最后一行搜索price。double的索引不能被取消引用吗?
我开始寻找结果,并得到一本书的书名。当用户输入关键字时,它将找到该书并显示该书的作者、价格和标题。我的主要问题是我的价值价格,我试图做索引,并将结果降低到一本书。如有任何建议,将不胜感激Java 在我的第二个名为下一个类的类中,公共数组列表的最后一行搜索price。double的索引不能被取消引用吗?,java,Java,我开始寻找结果,并得到一本书的书名。当用户输入关键字时,它将找到该书并显示该书的作者、价格和标题。我的主要问题是我的价值价格,我试图做索引,并将结果降低到一本书。如有任何建议,将不胜感激 public ArrayList<Test> searchForTitle( String searchString ) { ArrayList<Test> searchResult = new ArrayList<Test>(); for ( Book
public ArrayList<Test> searchForTitle( String searchString )
{
ArrayList<Test> searchResult = new ArrayList<Test>();
for ( Book currentBook : library )
{
if ( ( currentBook.getTitle() ).indexOf( searchString ) != -1 )
searchResult.add( currentBook );
}
searchResult.trimToSize();
return searchResult;
}
public ArrayList<Test> searchForAuthor( String searchString )
{
ArrayList<Test> searchResult = new ArrayList<Test>();
for ( Test currentBook : library )
{
if ( ( currentBook.getAuthor() ).indexOf( searchString ) != -1 )
searchResult.add( currentBook );
}
searchResult.trimToSize();
return searchResult;
}
next class:
public static void main(String [] args ) {
BookStore bs = new BookStore();
String keyword = JOptionPane.showInputDialog(null, "Enter a keyword" );
System.out.println("Our book collection is:");
System.out.println(bs.toString());
ArrayList<Test> results = bs.searchForTitle(keyword);
System.out.println("The search results for " + keyword + " are:");
for( Test tempBook : results )
System.out.println(tempBook.toString());
String Author = JOptionPane.showInputDialog(null, "Enter Author" );
results = bs.searchForAuthor(Author);
System.out.println("The search results for " + Author + " are:");
for ( Book tempBook : results)
System.out.println(tempBook.toString());
double lowestprice = Math.min(price);
String Lowest = JOptionPane.showMessageDialog(null,
"The lowest price in the bookstore is: " + lowestprice);
}
}
public ArrayList<Test> searchForPrice( String searchString )
{
ArrayList<Test> searchResult = new ArrayList<Test>();
for ( Book currentBook : library )
{
if ( ( currentBook.getPrice() ).indexOf( searchString ) != -1 )
searchResult.add( currentBook );
}
searchResult.trimToSize();
return searchResult;
}
}
public Test()
{
title = "";
author = "";
price = 0.0;
}
public Test(String newTitle, String newAuthor, double newPrice )
{
title = newTitle;
author = newAuthor;
price = newPrice;
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public double getPrice()
{
return price;
}
public String toString()
{
return ("title: " + title + "\t" + "author: " + author + "\t" + "price: " + price );
}
}
我认为这句话:
if ( ( currentBook.getPrice() ).indexOf( searchString ) != -1 )
您在双精度值内搜索字符串,因为SearchStrings来自类型Strings,GetPrice返回双精度值。这不是一个好的解决方案 您的getPrice方法返回一个double。但你要这样做:
if ( ( currentBook.getPrice() ).indexOf( searchString ) != -1 )
…它尝试在调用getPrice的结果上调用.indexOf。double是一种基本类型,它没有方法,如果有,它就没有indexOf
如果确实要将搜索项应用于双精度值,则必须将其转换为字符串:
if ( String.valueOf( currentBook.getPrice() ).indexOf( searchString ) != -1 )
// ^^^^^^^^^^^^^^
但对照价格检查搜索字符串似乎有点奇怪。是否显示不能取消引用双精度值?不要让我们猜:1。确切的错误消息是什么,以及2。该错误消息指的是哪一行?代码中的错误如何确切地称为编码风格问题?请不要标记垃圾邮件。请先更正标题,使您的问题具体化,并请参考至少产生错误的方法。我们没有时间阅读你所有的代码好吧,让我重新表述一下,我需要做的主要工作是以最低的价格将书返还给用户,这是一种通过图书馆查找作者书籍的方法,然后返还此类书籍的列表。第二种方法返回低于给定数字的图书价格。我应该从哪里开始聚焦,先生。