3

I am having a big problem with this, now I have a class called FileRelationship, and I have two constructors with it. Class is shown below. I also overrode the equals method in this class.

public class FileRelationship {
String fileName;
String firstLink;
String secondLink;
String thirdLink;

public  FileRelationship(String fileName) {
    this.fileName = fileName;
}

public  FileRelationship(String fileName, String firstLink, String secondLink, String thirdLink) {
    this.fileName = fileName;
    this.firstLink = firstLink;
    this.secondLink = secondLink;
    this.thirdLink = thirdLink;
}

public boolean equals(Object o) {
    if(o == null) {
        return false;
    }

    if(this == o) {
        return true;
    }

    if(o instanceof FileRelationship) {
        return this.fileName.equals(((FileRelationship)o).fileName);
    }

    return false;
}

}

Now I have an arrayList of FileRelationships and I want to be able to search through this list (These all use the constructor with 4 Strings in it (fileName, firstLink, secondLink, thirdLink)) Now I have a method in another class that is supposed to search this arrayList and this find the index of one that has a matching fileName. When I call this method I am only passing into the constructor with the fileName and that is all. (This is the only I can do it, I will not know the other strings).

This is how I set up the ArrayList of FileRelationships.

        fileRelationships = new ArrayList<FileRelationship>();

    String MasterLine;

    InputStream inputStream = this.getResources().openRawResource(R.raw.masterfile);

    InputStreamReader inputreader = new InputStreamReader(inputStream);
    BufferedReader buffreader = new BufferedReader(inputreader);

    // Creates the arrayList of all the relationships for later use of retreival.
    try {
        while (( MasterLine = buffreader.readLine()) != null) {
            MasterLineArray = MasterLine.split(",");
            String filename = MasterLineArray[0];
            String choice1 = MasterLineArray[1];
            String choice2 = MasterLineArray[2];
            String choice3 = MasterLineArray[3];
            FileRelationship fr = new FileRelationship(filename, choice1, choice2, choice3);

            fileRelationships.add(fr);
        }
    } catch (IOException e) {

    } finally {
        closeQuietly(buffreader);
}

This is my method for finding the relationship and then setting it to the variable I need. I want to be able to find the index of the list and apply it to the variable.

        public void findRelationship(String nextFileName) {
        int pageIndex = fileRelationships.indexOf((new FileRelationship(nextFileName)));
        selectedRelationship = fileRelationships.get(pageIndex);
        currentPage = pageIndex;
    }

EDIT : I'm sorry forgot to say what was wrong. No it doesn't work. pageIndex is returning a -1 (So in other words it isn't finding anything). I don't know what else to do, maybe use HashCode but honestly I've even looked up stuff about it and I can't really figure out how to use that either.

I really really need help on this, as this is for a school project due soon. I have most of the project done as once I get this part finished I should be almost done. Thanks.

6
  • 1
    What's the problem with this code? Doesn't it work? Commented May 5, 2017 at 6:29
  • I think u should use Iterator to search through the list or just use foreach loop if u don't want to do any modification. I think in your case value of pageIndex is -1. am i right? Commented May 5, 2017 at 6:35
  • Because I was trying to compare a FileRelationship(String, String, String String) with a FileRelationship(String) I figured I had to do so. Yes my pageIndex is -1 and that is the problem. Will that help return the index? Commented May 5, 2017 at 6:35
  • see my answer. And let me know if you still face any issue Commented May 5, 2017 at 6:47
  • How would I go about using an Iterator to do that and return the index of the list item. Commented May 5, 2017 at 6:48

1 Answer 1

2

Search like this

public void findRelationship(String nextFileName) {

for(FileRelationship file:fileRelationships)
{
    if(file.firsFileName.equals(nextFileName))
  {
     currentPage=fileRelationships.indexOf(file);
   }
}

}

list returns -1 when list doesn't contain that object. In your case you where using indexOf() obj by creating new object

 int pageIndex = fileRelationships.indexOf((new FileRelationship(nextFileName)));

this is not the right way. because you are creating new object and list does not contains new object

And also make getter setter method for each fields in FileRelationship class directly accessing each field is not the good practice

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.