Program must be written in Java: Write a program that performs the following tas
ID: 3687676 • Letter: P
Question
Program must be written in Java: Write a program that performs the following tasks: Repeatedly prompts the user for a name which will be a String. Store the name in an appropriate data structure to create a list of names. Develop a way to exit the repeated prompt. Ask the user the user for a name to search for in the data structure. Return the position of where the name is in the list and print the position to the user. The previous 2 tasks should occur in a menu that will let the user either add more names or search for names.Explanation / Answer
NameSearch.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class NameSearch {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
List<String> list = new ArrayList<String>();
while(true){
System.out.println("Choose your choice : 1) Add Name 2) Search Name 3) Print Names 4) Quit");
int choice = in.nextInt();
if(choice == 1){
addName(in, list);
}
else if(choice == 2){
searchName(in, list);
}
else if(choice == 3){
printNames(list);
}
else if(choice == 4){
break;
}
}
}
public static void addName(Scanner in, List<String> list){
System.out.println("Enter Name :");
String name = in.next();
list.add(name);
System.out.println("Given Name added successfully");
}
public static void searchName(Scanner in, List<String> list){
System.out.println("Enter Name for Search:");
String name = in.next();
int index = list.indexOf(name);
if(index != -1)
System.out.println("Given name found at index "+index);
else
System.out.println("Given name not found");
}
public static void printNames(List<String> list){
if(list != null && list.size() > 0){
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
}
else
System.out.println("No records available");
}
}
Output:
Choose your choice :
1) Add Name
2) Search Name
3) Print Names
4) Quit
1
Enter Name :
Suresh
Given Name added successfully
Choose your choice :
1) Add Name
2) Search Name
3) Print Names
4) Quit
3
Suresh
Choose your choice :
1) Add Name
2) Search Name
3) Print Names
4) Quit
1
Enter Name :
Sekhar
Given Name added successfully
Choose your choice :
1) Add Name
2) Search Name
3) Print Names
4) Quit
2
Enter Name for Search:
Naresh
Given name not found
Choose your choice :
1) Add Name
2) Search Name
3) Print Names
4) Quit
3
Suresh
Sekhar
Choose your choice :
1) Add Name
2) Search Name
3) Print Names
4) Quit
1
Enter Name :
Naresh
Given Name added successfully
Choose your choice :
1) Add Name
2) Search Name
3) Print Names
4) Quit
2
Enter Name for Search:
Sekhar
Given name found at index 1
Choose your choice :
1) Add Name
2) Search Name
3) Print Names
4) Quit
3
Suresh
Sekhar
Naresh
Choose your choice :
1) Add Name
2) Search Name
3) Print Names
4) Quit
4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.