SpatZan SpatZan - 29 days ago 5x
Java Question

Having trouble with indexOf(string name) method. Java

* return the index of the Predator with the given name. Case does not matter.

* No External Classes Permitted to Be Used in This Method Apart from
* String.Equals and String.ToUpperCase
* My Solution Length in Lines (note yours can be longer or shorter): 4
* @param name the name of the Predator to be found
* @return the position of the searched for Predator. -1 if none found

public int indexOf(String name) {


I know what I have to do but I am not sure how to go about solving it. So I know I have to loop through the array and see if the name equals the name in that element, if it does return the INDEX (the position). Can someone please help?


I guess you need to have the string array. All you need to do is to loop through every string and compare it with the name. If the case does not matter, you have to compare uppercased strings. Here is my proposition:

public int indexOf(String[] array, String name) { 
    for (int i=0; i<array.length; i++)
        if (array[i].toUpperCase().equals(name.toUpperCase()))
            return i;       
    return -1;