Ask Question

The following is intended to return the location of the first instance of the String the user enters from the keyboard, - 1 if not found.

String names [] = new String[20];

//assume array is initialized

System. out. println ("Enter a name to search for: ");

String lookingFor = scan. nextLine ();

int found = - 1;

for (int i = 0; i < names. length; i++) {

if ( / * Missing Code * / ) {

found = i;

break;

}

}

Which of the following could replace / * Missing Code * / so that it works as intended?

1. lookingFor. equals (names[i])

2.! lookingFor. equals (names[i])

3. lookingFor! = names[i]

4. lookingFor. equals names)

5. lookingFor [i]. equals (names[i])

+1
Answers (1)
  1. 15 August, 20:51
    0
    1. lookingFor. equals (Names[i])

    When comparing Strings, you wanna use. equals () cause String is a 'reference type'. (You'll use = = for primitive types).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “The following is intended to return the location of the first instance of the String the user enters from the keyboard, - 1 if not found. ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers