You are on page 1of 2

Rona Jean R.

Paloma
BSIT – 1B

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] names = new String[5];
int operation;
String searchName, deleteName;
boolean find;

for( int index = 0; index < 5; index++) {


System.out.print(“Enter Name: “);
names[index] = sc.nextLine();
}
do {
System.out.println(“\nOptions:”);
System.out.println(“1. Search”);
System.out.println(“2. Delete”);
System.out.println(“Press 0 to exit program!”);
System.out.print(“Select operation: “);
operation = sc.nextInt();
sc.nextLine(); // Consume newline

switch (operation) {
case 1:
System.out.print(“Search name: “);
searchName = sc.nextLine();
find = false;
for (int index = 0; index < 5; index++) {
if (names[index].equals(searchName)) {
System.out.println(searchName + “ found at index “ + index);
find = true;
break;
}
}
if (!find) {
System.out.println(searchName + “ not found”);
}
break;

case 2:
System.out.print(“Delete Name: “);
deleteName = sc.nextLine();
find = false;
for (int index = 0; index < 5; index++) {
if (names[index].equals(deleteName)) {
System.out.println(deleteName + “ deleted from index “ + index);
names[index] = null;
find = true;
break;
}
}
if (!find) {
System.out.println(deleteName + “ not found”);
}
break;
case 0:
System.out.println(“Exit.”);
default:
System.out.println(“Invalid choice”);
}
} while (operation != 0);
}
}

You might also like