Hi again, I have a class that is supposed to match different people of different genders together under different rules. For example, they are supposed to live in the same postal district. The problem is Java seems to be mismatching people, and I think its because it isn't comparing the strings correctly. Can you see anything wrong with the following class? Thanks.

public class Person{
String n;
double a;
double h;
String p;
String s;
String d;
String g;
public Person (String gender,String name,String pattern, String smoke, String district,double age,double height){
g=gender;
n=name;
a=age;
h=height;
p=pattern;
s=smoke;
d=district;

}
public String getG() {
    return this.g;
}
public String getN(){
return this.n;
}
public double getA(){
return this.a;
}
public double getH(){
return this.h;
}
public String getP(){
return this.p;
} 
public String getS(){
return this.s;
}
public String getD(){
return this.d;
}
public boolean checkMatch(Person other){
if (this.getA()<26&&this.getG().equals("male")&&other.getA()-this.getA()>1||
(this.getA()<26&&other.getG().equals("male")&&other.getA()-this.getA()>1)||
(!((this.getH()-other.getH()<25)))
||(!((other.getH()-this.getH())<25))){
return false;
}
else if (this.getG().equals(other.getG())){  
return false;
}
else if (!(this.getS().equals(other.getS()))){
return false;
}else if(this.getP().equals(other.getP())&&this.getD().equals(other.getD())){
return true;
}else{
return false;
}
}


public String findMatch(Person other,Person other2, Person other3,Person other4,Person other5){
String part1="";
String part2="";
String part3="";
String part4="";
String part5="";
if (this.checkMatch(other)==true){
part1= other.getN();
}else if(this.checkMatch(other2)==true){
part2= other2.getN();
}else if(this.checkMatch(other3)==true){
part3= other3.getN();
}else if(this.checkMatch(other4)==true){
part4= other3.getN();
}else if(this.checkMatch(other5)==true){
part5= other3.getN();
}
return (part1+" "+part2+" "+part3+" "+part4+" "+part5);
}
}

Hi,

If you are using equals() method to compare strings, Java compares the strings correctly.

Check again your checkMatch() findMatch() method and make sure that they are correct.
Add more brackets in your IF statements, if needed, to make sure the precedence of operators is the one you want.

If we don't know the exact matching rules we cant help you find the problem.

GL

Hi,
I rewrote both methods and found my problem. In my findmatch method I was returning the same name for the last three cases!
Thanks for your time,
Bob

This question has already been answered. Start a new discussion instead.