56.7k views
10 votes
In java please

In this exercise, you will need to create a static method called findString in the MatchingString class that should iterate over String[] arr looking for the exact match of the String that is passed as a parameter.

Return the index of the array where the String is found; if it does not exist in the array, return -1.

For example, if the word “Karel” is passed in, your method would return 1.

User DzOrdre
by
3.7k points

1 Answer

12 votes

Answer:

Step-by-step explanation:

The following code is written in Java. It is a static method that takes in a String parameter and loops through the String array called arr comparing each element in the array with the word parameter that was passed. If it finds a match the method returns the index of that word, otherwise it will return -1

public static int findString(String word){

int index = -1;

for (int x = 0; x < arr.length; x++) {

if (word == arr[x]) {

index = x;

break;

}

}

return index;

}

User NiCk CAMel
by
3.9k points