Write a method, called findNameIndex, which receives two parameters: 1: an array
ID: 3858129 • Letter: W
Question
Write a method, called findNameIndex, which receives two parameters: 1: an array of type String, called nameArray, 2: a String called firstName and returns an integer value. The method searches the nameArray for the given firstName. If the firstName matches one of the values stored in the nameArray, the method returns the index of the matched name in the array. If the firstName is not found in the nameArray, the method returns -1.
Test your method by writing a Java program that its main method does the following:
a) Creates a one-dimensional array, called peopleNames, of type String, and fills the array with the first names of 5 people.
b) Prints the peopleNames array.
c) Calls findNameIndex and passes the peopleNames array and a first name of type String to the method for the following two cases and prints the result:
i- The given first name does not exist in the peopleNames array.
ii- The given first name exists in the peopleNames array.
Explanation / Answer
Code :
class StringSearch{
Output for if firstNmae is "Alies" :
The 5 first names are
David, Alies, Sony, Dumblour, Parits,
The given first name exists in the peopleNames array.
Output for if firstNmae is other than array elements :
The 5 first names are :
David, Alies, Sony, Dumblour, Parits,
The given first name does not exist in the peopleNames array.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.