I'm working on sequential search and this is my code to search a String. The problem I have is that once the array is found, it stop. I was wondering what if there are more than one occurrence of that String in the array. I'm trying to modify my code so it find all the occurence and print it. I thought of removing the "break" but then even if I remove it, the code would replace the "found" index by the next occurence that it find, so my program results in printing the last match that it find instead of all of them. Can anyone help me fix this? Thanks
public static void findPerson(Assignment[] r, String toFind) { int found = -1; for(int i = 0; i < r.length; i++) if (r[i].getPerson().compareTo(toFind) == 0) { found = i; break; } if (found != -1) { // we have found the person System.out.println("We found " + toFind + " in the roster: "); System.out.println(r[found]); } else System.out.println(toFind + " is not in the roster"); }