Reputation: 9
this is my first question. Im a studying business administration and started to learn java just for fun. I'm doing the MOOC course from a finnish university. I am at exercise 101. I have to program a library with a search function for books. One can either search by title, publisher or year.
My only ideas were those (that did not work):
public ArrayList<Book> searchByPublisher(String publisher){
ArrayList<Book> found = new ArrayList<Book>();
if (bibo.contains(publisher)){
found.add(bibo.get(bibo.indexOf(publisher)));
}
return found;
or:
public ArrayList<Book> searchByTitle(String title){
ArrayList<Book> found = new ArrayList<Book>();
String [] neueListe = new String[bibo.size()];
for (int i = 0; i < bibo.size(); i++){
neueListe[i] = bibo.get(i).toString();
found.add(neueListe[i].toString());
}
return found;
"bibo"
is the ArrayList
with the books.
I've searched the internet for ideas and solutions and of course came across several on GitHub. But every code I found used the same idea:
for(Book i : this.bibo){
if(StringUtils.included(i.title(), title)){
found.add(i);
}
In the course there was never used the StringUtils.included
and my NetBeans version doesn't know it.
Where the heck is my error in reasoning?
Thanks a lot already in the first place :-)
Upvotes: 0
Views: 124
Reputation: 5481
StringUtils is a class from the org.apache.commons.lang library
Upvotes: 0
Reputation: 7482
The problem in
public ArrayList<Book> searchByPublisher(String publisher){
ArrayList<Book> found = new ArrayList<Book>();
if (bibo.contains(publisher)){
found.add(bibo.get(bibo.indexOf(publisher)));
}
return found;
is that you are asking if bibo
contains a String
while you should ask if it contains a book which publisher field equals to that String (the input to searchByPubisher).
public ArrayList<Book> searchByPublisher(String publisher){
ArrayList<Book> found = new ArrayList<Book>();
for(Book b: bibo){
if(b.getPublisher().equals(publisher))
found.add(b);
}
return found;
}
The searchByTitle
function can be implemented similarly.
I would also mention that you can also use Streams
to do the job.
(Since you are learning java
) something like the following should be enough to let you make it work by yourself:
List<Book> found =
bibo.stream()
.filter(b -> t.getPublisher().equals(publisher))
.collect(toList());
Hope this helps.
Upvotes: 0
Reputation: 21
I would do it this way.
public ArrayList<Book> searchByTitle(String title){
ArrayList<Book> found = new ArrayList<Book>();
for (Book book: bibo) {
if(book.title.contains(title)) {
found.add(book);
}
}
return found;
You could also look at the Java 8 Streams and the filter functions when you have more java experience. Java 8 Streams example
Upvotes: 0
Reputation: 852
The StringUtils
is a third part library form Apache Commons Lang
. if you want to use this class you should add the Apache Commons Lang
to your project in the Netbeans.
This tutorial shows how to add Apache Commons in Netbeans:
https://www.satollo.net/netbeans-apache-commons-and-commons-javadoc-for-auto-completion
Upvotes: 1