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?
{ for(int i = 0; i < giftGivers.length; i++) if (...) { return i; } }or{ int i = 0; while(i < giftGivers.length && !giftGivers[i].name.equals(n)) { i++; } return i; }