static int findPerson(String n, int NP, Friend[] giftGivers){
int index = 0;
for (int i = 0; i < NP; i++){
if (giftGivers[i].name == n){
index = i;
}
}
return index;
}
I have this code in Java for a method to search through an array of Friends to find the index number of the person with the name input by String n. however i have found that the index number does not set to the index number it is should be. Is it because it is in the if statement?
if (giftGivers[i].name == n)is wrong, useif (giftGivers[i].name.equals(n))BTW, there is no need to use
NP. It’s C-style, not necessary (actually, pretty dangerous) in Java. Instead offor (int i = 0; i < NP; i++),just say
for (int i = 0; i < giftGivers.length; i++)