user10299100
user10299100

Reputation:

A search method that finds a string value in an String Array

So I am trying to create a method that finds a title in a ArrayList of photos.

public class Album {

    private String albumtitle;
    private ArrayList<Photo> photos;

    /**
     * This constructor should initialize the
     * instance variables of the class.
     */
    public Album(String title) {

        this.albumtitle = title;
        photos = new ArrayList<>();

    }

This is the code I have got to for trying to search for a specific title of the photo. I am not sure if i should put (int index) or (String title) in the methods parameters.

public Photo searchByTitle(int index) {

        if (index >= 0 && index < photos.size()) {
            String title = photos.get(index);
            System.out.println(title);
        }
        return null;
    }

I am beginner programmer, and I feel a little guidance will help a lot.

Edit: So lots of people are telling me to use for loops. My project requires me to not do for loops for methods, hence why I have displayed it in this way.

I'll give you an example the lecturer gave us:

https://lms.uwa.edu.au/bbcswebdav/pid-1134902-dt-content-rid-16529804_1/courses/CITS1001_SEM-2_2018/lectures/BooksReadJournal.java.pdf

She doesn't use for loops.

Upvotes: 3

Views: 442

Answers (5)

Avvappa Hegadyal
Avvappa Hegadyal

Reputation: 273

package simple;
import java.util.ArrayList;
public class Album {
private static String albumtitle;
public Album(String title) {
    Album.albumtitle = title;
}

public Photo searchByTitle(ArrayList<Photo> photos) {
    if (index >= 0 && index < photos.size()) {
        Photo title = photos.get(index);
        if (title.getTitle().equals(albumtitle))
            System.out.println("tile Found" + title);
    }
    return null;
}

public static void main(String[] args) {
    ArrayList<Photo> photos = new ArrayList<>();
    //Searching title2 or u can pass as parameter
    Album album = new Album("title2");
    for (int i = 0; i < 5; i++) {
        Photo photo = new Photo();
        photo.setTitle("Title" + i);
        photos.add(photo);

    }
    album.searchByTitle(photos);

}

}

package simple;
public class Photo {
String title;
public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

}

Are looking for this..?

Upvotes: 0

deHaar
deHaar

Reputation: 18558

You have two options for a search, either you look a Photo up by its title or by its index. If your class had both methods, it would look like this:

public class Album {

    private String albumtitle;
    private ArrayList<Photo> photos;

    /**
     * This constructor should initialize the instance variables of the class.
     */
    public Album(String title) {
        this.albumtitle = title;
        photos = new ArrayList<>();
    }

    /**
     * Searches the {@link Photo} with the given title.
     * @param title the title of the desired {@link Photo}
     * @return the {@link Photo} with the given title or 
     *  <code>null</code> if it is not in the list
     */
    public Photo searchByTitle(String title) {
        // initialize with null to return that if the photo was not found
        Photo photo = null;

        // iterate all photos and check if one of them has the title
        for (int i = 0; i < photos.size(); i++) {
            if (title.equals(photos.get(i).getTitle())) {
                photo = photos.get(i);
                // exit the loop when the photo was found
                break;
            }
        }

        return photo;
    }

    /**
     * Searches the {@link Photo} with the given index.<br>
     * Checks if the index is valid in the list of {@link Photo}s
     * @param index the index of the {@link Photo}
     * @return the {@link Photo} with the given index or <code>null</code> 
     *  if it is not in the list or the index is invalid.
     */
    public Photo searchByIndex(int index) {
        try {
            // use the method of the list to get the photo
            return photos.get(index);
        } catch (IndexOutOfBoundsException ioe) {
            // print some error message for the case of an invalid index
            System.err.println("The given index is not available!");
            return null;
        }
    }
}

Upvotes: 1

ItFreak
ItFreak

Reputation: 2369

You could use the stream-API:

Arrays.stream(photos).filter(p -> p.getTitle().equalsIgnoreCase(searchTitle)).findFirst().orElseGet(...);

You iterate over each photo called p in this array and compare p's title with the one you search and return the first match.

Or simply use a normal for-loop:

for(int i = 0; i < photos.size(); i++) {
  if(photos[i].getTitle().equalsIgnoreCase(searchTitle)){ return photos[i]; }
  return new ErrorPhoto(); //or some error state
}

More enhanced for-each-loop:

for(Photo p: photos) {
  if(p.getTitle().equalsIgnoreCase(searchTitle)) { return p; }
  return new ErrorPhoto(); //or some error state
}

Upvotes: 4

user3437460
user3437460

Reputation: 17454

Your current code will always return null. If this is a search method to return the found object, you return the object if it is found. If not found, then return null:

public Photo searchByTitle(String title) {    
    for(Photo p : photos)
        if(p.getTitle().equals(title))
            return p;
    return null;
}

This is the code I have got to for trying to search for a specific title of the photo. I am not sure if i should put (int index) or (String title) in the methods parameters

Since the method name itself suggested searchByTitle, whoever is using this method would expect it to receive String title.

If it is to be searched by index, then there can be another method as such:

public Photo searchByIndex(int index){

}

Upvotes: 2

Faraz
Faraz

Reputation: 6265

You iterate over each photo and compare its title with the one you search and return the first match:

for(int i = 0; i < photos.size, i++) {
    if(photos.get(i).getTile().equals("title name"){
         System.out.println("Found the title");
    }
}

Upvotes: 3

Related Questions