Reputation: 25
I'm trying to create a registry in form of an ArrayList which shall save all kinds of books I've read. The program has only two classes. One as a template for the entries (Name: Entry) and one which manages the entries in the ArrayList (Name: Registry).
These are the attributes of Entry:
private final String title;
private final String mangaka;
private final String year;
private final String genre;
private final String volumes;
private String completelyScanlated;
private String licensedInGerman;
private String read;
private String comments;
and thus has this constructor:
public Entry(final String title, final String mangaka, final String year, final String genre, final String volumes,
String completelyScanlated, String licencedInGerman, String read, String comment)
{
this.title = title;
this.mangaka = mangaka;
this.year = year;
this.genre = genre;
this.volumes = volumes;
this.completelyScanlated = completelyScanlated;
this.licensedInGerman = licensedInGerman;
this.read = read;
this.comments = comments;
}
The class "Registry" has only one attribute:
ArrayList<Entry> entries = new ArrayList<Entry>();
The user creates an entry via 'Scanner' therefore by typing Strings into the console. The created object is saved in the ArrayList via:
Entry object = new Entry(title, mangaka, year, genre, volumes, completelyScanlated, licensedInGerman, read, comments);
entries.add(object);
Now I want to check whether a String (which is also created with a console input) is equal to the attribute "title". I could check the equality of an input with the method ".contains()" but this method would compare all attributes. Is there a way to check only one attribute?
Here is the non-working code:
public void findEntry()
{
Scanner input = new Scanner(System.in);
System.out.println("Which title do you want to search for?");
String searchedEntry = input.nextLine();
System.out.println();
if (entries.contains(searchedEntry)) {
int x = entries.indexOf(searchedEntry);
entries.get(x);
//Entry.showDetails();
}
}
The result is to given out via the console (that code is working though).
Thanks in advance
Upvotes: 1
Views: 65
Reputation: 1498
Instead of
if (entries.contains(searchedEntry)) {
int x = entries.indexOf(searchedEntry);
entries.get(x);
//Entry.showDetails();
}
You can iterate through entries. Something like this:
for(Entry entry: entries) {
if(entry.getTitle().equals(searchedEntry)) {
// Do whatever it has to do.
}
}
If title doesn't need to match the case, you can use:
if(entry.getTitle().equalsIgnoreCase(searchedEntry))
Upvotes: 0
Reputation: 4039
I assume you have a getter for your Title in your Entry class. Something like getTitle(). Then iterate over all entries and check if its title contains the search string.
public void findEntry(){
Scanner input = new Scanner(System.in);
System.out.println("Which title do you want to search for?");
String searchedEntry = input.nextLine();
System.out.println();
for(Entry entry : entries){
if(entry.getTitle().contains(input))
entry.showDetails(); // or Whatever
}
}
Even better whould it be to convert the title and the search string to lower cases befor.
if(entry.getTitle().toLowerCase().contains(input.toLowerCase()))
So you can search for "lord of THE rings" and will find "The Lord of the Rings: The Fellowship of the Ring".
Upvotes: 1