Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Solomon Alexandru
3,633 PointsSearch functionality
I'm trying to create a method that offers the user the possibility to search onto an object list a string/integer. I don't know what I'm doing wrong, the console returns nothing. The main class:
import java.util.ArrayList;
public class Randomizing {
public static void main(String[] args) {
Library Library = new Library();
Library.addBook(new Book("Cheese Problems Solved", "Woodhead Publishing", 2007));
Library.addBook(new Book("The Stinky Cheese Man and Other Fairly Stupid Tales", "Penguin Group", 1992));
Library.addBook(new Book("NHL Hockey", "Stanley Kupp", 1952));
Library.addBook(new Book("Battle Axes", "Tom A. Hawk", 1851));
ArrayList<Book> result = Library.searchByTitle("Cheese");
for (Book book: result) {
System.out.println(book);
}
System.out.println("---");
for (Book book: Library.searchByPublisher("Penguin Group ")) {
System.out.println(book);
}
System.out.println("---");
for (Book book: Library.searchByYear(1851)) {
System.out.println(book);
}
}
}
The book class:
public class Book {
private String title;
private String publisher;
private int year;
public Book(String title, String publisher, int year) {
this.title = title;
this.publisher = publisher;
this.year = year;
}
public String title() {
return this.title;
}
public String publisher() {
return this.publisher;
}
public int year() {
return this.year;
}
public String toString() {
return this.title + ", " + this.publisher + ", " + this.year;
}
}
And the library class:
import java.util.ArrayList;
public class Library {
private ArrayList<Book> list;
public Library() {
this.list = new ArrayList<Book>();
}
public void addBook(Book newBook) {
list.add(newBook);
}
public void printBooks() {
for(Book boo : this.list) {
System.out.println(boo);
}
}
public ArrayList<Book> searchByTitle(String title) {
ArrayList<Book> found = new ArrayList<Book>();
for(Book book : found) {
if(book.title().contains(title)) {
found.add(book);
}
}
return found;
}
public ArrayList<Book> searchByPublisher(String publisher) {
ArrayList<Book> found = new ArrayList<Book>();
for(Book book : found) {
if(book.publisher().contains(publisher)) {
found.add(book);
}
}
return found;
}
public ArrayList<Book> searchByYear(int year) {
ArrayList<Book> found = new ArrayList<Book>();
for(Book book : found) {
if(book.year() == year) {
found.add(book);
}
}
return found;
}
}
If anyone could please help me, I would be very grateful. Thank you!
Solomon Alexandru
3,633 PointsAlright, waiting for your response
2 Answers
Chris Howell
Python Web Development Techdegree Graduate 49,703 PointsI created a workspace modifying your code and took a snapshot of it. Snapshot link
I was running a bit low on time, so the code could definitely use some love. These are definitely not best practices. If you have questions about what is happening ask away.
I commented out some of your lines of code for function calls, because I wrote some new functions that use some pattern matching for searches. But generally for searching, RegEx is a powerful and great way to go. There actually may be packages just for doing this. I am not familiar enough with Java to point those out though someone else may be.
Solomon Alexandru
3,633 PointsChris, I've tried to read the code but unfortunately the snapshot has a bug.
Chris Howell
Python Web Development Techdegree Graduate 49,703 PointsSo once you compile your .java using javac, the compiled output is turned into a .class file which is readable for the computer not for you.
Use the .java files to read the "human readable" code :)
Solomon Alexandru
3,633 PointsOh yes, haha I'm such a newb :) I don't undertand the use of the following codes:
target = target.replaceAll("^(\\s+)*|(\\s+)*$", "");
Pattern patt = Pattern.compile("(.*)"+target+"(.*)");
A lot of new features up there. Need to learn something new :P
Chris Howell
Python Web Development Techdegree Graduate 49,703 PointsSo before I added those lines, the word or phrase you would type in wasn't accounting for spaces before or after. So the first line is replacing all "space characters" in regex is expressed with the two backslashes followed by s. The spaces are being replaces with an empty string. So basically it is removing any space characters from before and after whatever word was passed in.
The second line is using a library that compiles the regular expression that is contained inside the compile method.
it basically is saying "find me matches that find any character followed by this target word followed by any character.
Solomon Alexandru
3,633 PointsI believe you used some kind of converter? Could you please tell me which one, so I can analyze it more deeply? I tried to read the documentation but it's hard to understand for a beginner.
Chris Howell
Python Web Development Techdegree Graduate 49,703 PointsI just included the Pattern and Matcher classes that are built into Java utilities class under regex package.
I am also still learning java so I had to use a few sources for "how it is used".
Besides the Java Docs. Here was a few other sites I ended up on that I had to do reading on.
I found these helpful.

Chris Howell
Python Web Development Techdegree Graduate 49,703 PointsChris Howell
Python Web Development Techdegree Graduate 49,703 PointsI will have a look through and see where I get with it.