Untitled
unknown
plain_text
a year ago
2.7 kB
7
Indexable
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class StringListOperations { public static void main(String[] args) { List<String> stringList = new ArrayList<>(); Scanner scanner = new Scanner(System.in); int choice; do { System.out.println("\nMenu:"); System.out.println("1. Insert a string"); System.out.println("2. Delete a string"); System.out.println("3. Display all strings"); System.out.println("4. Search for a string"); System.out.println("5. Exit"); System.out.print("Enter your choice: "); choice = scanner.nextInt(); scanner.nextLine(); // Consume newline switch (choice) { case 1: System.out.print("Enter a string to insert: "); String insertString = scanner.nextLine(); stringList.add(insertString); System.out.println("String inserted successfully!"); break; case 2: System.out.print("Enter the index of the string to delete: "); int deleteIndex = scanner.nextInt(); if (deleteIndex >= 0 && deleteIndex < stringList.size()) { String deletedString = stringList.remove(deleteIndex); System.out.println("Deleted string: " + deletedString); } else { System.out.println("Invalid index. No string deleted."); } break; case 3: System.out.println("Strings in the list:"); for (String s : stringList) { System.out.println(s); } break; case 4: System.out.print("Enter a string to search: "); String searchString = scanner.nextLine(); if (stringList.contains(searchString)) { System.out.println("String found in the list."); } else { System.out.println("String not found in the list."); } break; case 5: System.out.println("Exiting program. Goodbye!"); break; default: System.out.println("Invalid choice. Please select a valid option."); } } while (choice != 5); scanner.close(); } }
Editor is loading...
Leave a Comment