Write a java program to search given name into the array, if it is
found then display its index otherwise display appropriate
message.
public class NameSearch {
public static void main(String[] args) {
// Sample array of
names
String[] names =
{"Asha", "Babita", "Chmpa", "David",
"Eina"};
// Name to
search
String searchName
= "Charlie"; // Change this to the name you want to search
int index =
-1; // Initialize the index to -1 (not found)
// Search
for the name in the array
for (int i = 0; i
< names.length; i++) {
if
(names[i].equals(searchName)) {
index
= i; // Name found, store the index
break;
// Exit the loop
}
}
// Display
the result
if (index != -1) {
System.out.println("Name
'" + searchName + "' found at index " + index);
} else {
System.out.println("Name
'" + searchName + "' not found in the array.");
} }}