0% found this document useful (0 votes)
15 views

Worksheet-2.3 Java PDF

3.4

Uploaded by

rohitjangra7944
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Worksheet-2.3 Java PDF

3.4

Uploaded by

rohitjangra7944
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment- 2.3

Student Name: Lakshay Sindhu UID: 21BCS10420


Branch: CSE Section/Group: 21BCS CC 628 - B
Semester: 6th (Sixth) Date of Performance: 29/02/24
Subject Name: Project Based Learning in Java Lab Subject Code: 21CSH-319

1. Aim : Write a Program to perform the basic operations like insert, delete, display and search
in list. List contains String object items where these operations are to be performed..

2. Objective : This program aims to create a string list manager, allowing users to insert,
delete, display, and search for specific strings within the list. This provides a basic framework
for manipulating and organizing string data.

3. Procedure :
1. ArrayList and Scanner: The program uses an ArrayList to store items and a Scanner to read user
input.
2. Menu Display: A menu is continuously displayed to the user until they choose to exit. The menu
includes options to insert, search, delete, display items, and exit the program.
3. Switch Statement: Depending on the user’s choice, different operations are performed. This is
managed using a switch statements.
4. Insert Operation: If the user chooses to insert an item, they are prompted to enter the item. The item is
then added to the ArrayList.
5. Search Operation: If the user chooses to search for an item, they are prompted to enter the item. The
program then checks if the item exists in the ArrayList.
6. Delete Operation: If the user chooses to delete an item, they are prompted to enter the item. The
program then attempts to remove the item from the ArrayList
7. Display Operation: If the user chooses to display the items, the program prints all the items in the
ArrayList.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Code and Output :

Program Code :
import java.util.ArrayList;
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("Lakshay Sindhu, 21BCS10420");
System.out.println("1. Insert\n2. Search\n3. Delete\n4. Display\n5. Exit");
System.out.println("Enter your choice : ");
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.println("Enter the item to be inserted: ");
String item = scanner.nextLine();
list.add(item);
System.out.println("Inserted successfully");
break;
case 2:
System.out.println("Enter the item to search : ");
item = scanner.nextLine();
if (list.contains(item)) {
System.out.println("Item found in the list.");
} else {
System.out.println("Item not found in the list.");
}
break;
case 3:
System.out.println("Enter the item to delete : ");
item = scanner.nextLine();
if (list.remove(item)) {
System.out.println("Deleted successfully");
} else {
System.out.println("Item does not exist.");
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

break;
case 4:
System.out.println("The Items in the list are : ");
for (String str : list) {
System.out.println(str);
}
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice! Please enter a valid option.");
}
} while (choice != 5);
scanner.close();
}
}

Output :
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Learning Outcomes :

1. Learnt about ArrayList in java.

2. Learnt about different methods of ArrayList in java.

3. Learnt about advantages of ArrayList over Array.

4. Learnt about difference between ArrayList and Linked List.

You might also like