My project was already due, and incomplete but I would still like to better understand a few things.

I have three classes. A Photo Class, PhotoAlbum Class, and a CommandLineMenu class.

Each photo has a subject, location, date, and path.
the PhotoAlbum is an array of different photo objects. I needed to make a method to search for photos by location and return an array of 0 or more photos.

the only way I could get it to even search for the word was writing the Album data to a file and then reading that file. This didnt even return a photo object but only the line of the file where the string was found.

How can I achieve to return a photo?
Search method:

public void findByLocation()
	{
		Scanner instream = new Scanner(System.in);
		
		System.out.println("find by location");
        System.out.print("Enter Location: ");
String locale = instream.next();

try {
BufferedReader bf = new BufferedReader(new FileReader("photos" + "/" + "album.dat"));
int linecount = 0;
String line;
System.out.println("Searching for " + locale + " in file...");
while (( line = bf.readLine()) != null)
{
        // Increment the count and find the index of the word
        linecount++;
        int indexfound = line.indexOf(locale);

        // If greater than -1, means we found the word
        if (indexfound > -1) {
             String strung = "File found on line " + linecount;
             System.out.println (strung);
        }
}

// Close the file after done searching
bf.close();
}
catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());

}
	}

Post some lines of the file

the file all depends on what photos the user adds. So I just added two photos really quick and it stored them as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
My Photo Album

Number of Photos: 2

Photo List:

Subject: emily
Location: boise
Date: 02/14/2020
Path: photos/photo005.png

Subject: f
Location: f
Date: 08/08/0567
Path: photos/photo006.png

Does the file has to be like that? I mean is it you that decides how the data will be saved?

If you cannot change the way the data are saved then try this:

From what I see you know how to read line by line a file:

while (( line = bf.readLine()) != null)
{
   System.out.println("Line read: "+line);       
}

Create a class Photo like this:

class Photo {

 private String subject = null;
 private String location = null;
 private String date = null;
 private String path = null;

 public Photo() {

 }

// add get/set methods
}

If the file will always have correct data then:
Read until you find the number of photos:

int numOfPh = 0;
Photo [] photos = null;

while () {
 if (line.startsWith("Number of Photos:")) {
     numOfPh = ... ; //get the number.
     photos = new Photo[numOfPh];
 }
}

Then read until you find the line with the "Subject:" . Assuming that the file will always has those 4 attributes with that order then, after you find the line with the "Subject" read the next 4 lines:

int index = 0;
while () {
  if (line.startsWith("Subject")) {
      String subj = line; // get the subject from the line;
      String loc = bf.readLine(); // get the location from the line
      String date = bf.readLine(); // get the date from the line
      String path = bf.readLine(); // get the path from the line

      photos[index] = new Photo();
      // set those values to the photos[index]
     photos[index].setSubject(subj);
     .....
     index++;
  }
}

And at the end of the method return the photos array.

Now as far as getting the values. The line has this value:

line = "Subject: emily";
line = "Location: boise";

So you cannot do this:

String subj = line; // get the subject from the line;
String loc = bf.readLine(); // get the location from the line

But you need to write a method that takes as argument a String like this: "Name: value" and extract the value. You will call that method to get the Number of Photos as well:

public Stirng getValue(String s) { // s = "Subject: emily"
    // method will return emily
}

You can look the java.lang.String API. There is a method indexOf that returns the position of the first occurrence of a specific String: int indexOf = s.indexOf(":"); Now that you know where the ':' is , you can call the substring method of the String class and get what is after the index:
Subject: emily -> substring after the : will return "emily".Don't forget to take into account the "empty space" after the ':'


Else if you are the one who decides how the data are saved, then there is a better way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.