fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Main{
  5. public static void main(String[] args){
  6. int choice;
  7. List<String> list=new ArrayList<String>();
  8. Iterator itr=list.iterator();
  9. try{
  10. do{
  11. //BufferedReader br1=new BufferedReader(r);
  12. System.out.println("1. Insert");
  13. System.out.println("2. Search");
  14. System.out.println("3. Delete");
  15. System.out.println("4. Display");
  16. System.out.println("5. Exit");
  17. System.out.println("Enter your choice :");
  18. choice=Integer.parseInt(br.readLine());
  19. if(choice==1){
  20. System.out.println("Enter the item to be inserted:");
  21. String insert=br.readLine();
  22. //br.readLine();
  23. list.add(insert);
  24. System.out.println("Inserted successfully");
  25. }
  26. else if(choice==2){
  27. System.out.println("Enter the item to search :");
  28. String search=br.readLine();
  29. int flag=0;
  30. for(String i:list){
  31. if(i.equals(search)){
  32. System.out.println("Item found in the list.");
  33. flag=1;
  34. }
  35. }
  36. if(flag==0){
  37. System.out.println("Item not found in the list.");
  38. }
  39. }
  40. else if(choice==3){
  41. System.out.println("Enter the item to delete :");
  42. String delete=br.readLine();
  43. //br.readLine();
  44. int flag=0;
  45. for(String i:list){
  46. if(i.equals(delete)){
  47. list.remove(delete);
  48. System.out.println("Deleted successfully");
  49. flag=1;
  50. }
  51. }
  52. if(flag==0){
  53. System.out.println("Item does not exist.");
  54. }
  55.  
  56. }
  57. else if(choice==4){
  58. System.out.println("The Items in the list are :");
  59. for(String i:list){
  60. System.out.println(i);
  61. }
  62. }
  63. else{
  64. return;
  65. }
  66. //r.close();
  67. //br.close();
  68. }while(choice!=5);
  69. }
  70. catch(IOException e){
  71. e.printStackTrace();
  72. }
  73. }
  74.  
  75. }
Success #stdin #stdout 0.1s 320576KB
stdin
1
water
1
cap
1
little
2
cap
3
cap
4
5
stdout
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your choice :
Enter the item to be inserted:
Inserted successfully
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your choice :
Enter the item to be inserted:
Inserted successfully
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your choice :
Enter the item to be inserted:
Inserted successfully
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your choice :
Enter the item to search :
Item found in the list.
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your choice :
Enter the item to delete :
Deleted successfully
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your choice :
The Items in the list are :
water
little
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your choice :