0

I'm having problems accessing a field from an array in a for-loop. I'm sure I'm just messing up the syntax:

public class Person {

String  name;
Person  mother;
Person  father;
ArrayList<Person> children;

public boolean isMotherOf(Person pers1) {
    //First check if Persons mother equals pers1
    if (mother.name == pers1.name) {            
        //Second checks if pers1s children contains Person
        for (int i = 0; i < pers1.children.size(); i++) {
            if (pers1.children.name.get(i) == name) {
// ERROR ON THE LINE ABOVE: "name cannot be resolved or it not a field"
                return true;
            } 
        } // END second check
    } else { return false; }


    } // END method isMotherOf
}

EDIT: My code contained logic errors (compared the wrong persons). Will this safety check work, or will it yield an error if pers1 does not exist when it checks if pers1's mother has a name?

public class Person {

String  name;
Person  mother;
Person  father;
ArrayList<Person> children;

// Checks if THIS person is the mother of pers1
public boolean isMotherOf(Person pers1) {
    // Safety check: Both persons exists and Person has children and pers1 has mother 
    if (name == null || children == null || pers1 == null
        || pers1.mother.name == null) {
        return false;
    } // END safety check
    // First check if Persons name equals pers1's mother
    else if (name.equals(pers1.mother.name)) {          
        // Second check if Persons children contains pers1
        for (int i = 0; i < children.size(); i++) {
            if (children.get(i).name.equals(pers1.name)) {
                return true;
            } 
        } // END second check
    } // END first check 
    return false;
} // END method isMotherOf
} // END class Person
user1803704
  • 27
  • 1
  • 1
  • 4
  • Unrelated to the question, you should compare strings using the `equals` function, not `==`. Otherwise you'll find the comparison almost never works -- see http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java for more info. – yshavit Feb 01 '13 at 14:53

3 Answers3

4

You swapped two things, pers1.children.name.get(i) should be pers1.children.get(i).name.

Since we're here, you are comparing strings with ==, this compare references of objects, use equals(..) instead. Check it out here.

Jack
  • 131,802
  • 30
  • 241
  • 343
0

Looks like you're using == instead of equals() function to compare strings.

Try changing this line like the following:

if (pers1.children.name.get(i).equals(name))
Zathrus Writer
  • 4,311
  • 5
  • 27
  • 50
0

Don't use "==" for String comparison; it's not doing what you think. You want to write it as:

if (mother.name.equals(pers1.name)) { ...

Of course, that's a slightly dangerous approach, as you'll get NullPointerExceptions if either mother or mother.name are null. So you'll want to do a little defensive programming.

if (mother == null) return false;
if (pers1 == null) return false;
if (mother.name != null && mother.name.equals(pers1.name)) ....
BlairHippo
  • 9,502
  • 10
  • 54
  • 78