Chapter 19: Problem 13
Write a program that reads in a series of first names and stores them in a LinkedList. Do not store duplicate names. Allow the user to search for a first name.
Short Answer
Expert verified
Create a LinkedList, read names with duplicates check, and use `contains` for search.
Step by step solution
01
Setting Up Your LinkedList
First, create a LinkedList to store the first names. We'll also need to import the LinkedList class from the Java `util` package which provides the necessary methods to manipulate the linked list.
02
Reading User Input
Use a scanner to read user input. Create a loop to continually ask the user for first names until they enter a specific keyword (like 'exit') to stop.
03
Checking for Duplicates
Inside the loop, before adding a name to the LinkedList, check if the name already exists in the list using the `contains` method. If it doesn't exist, add the name to the list with the `add` method.
04
Allowing User to Search for a Name
After exiting the name input loop, ask the user to enter a first name they wish to search for in the LinkedList. Use the `contains` method to check if the name is present in the LinkedList.
05
Displaying Search Results
If the searched name is found in the LinkedList, print a message confirming its existence; otherwise, indicate that the name is not in the list.
Unlock Step-by-Step Solutions & Ace Your Exams!
-
Full Textbook Solutions
Get detailed explanations and key concepts
-
Unlimited Al creation
Al flashcards, explanations, exams and more...
-
Ads-free access
To over 500 millions flashcards
-
Money-back guarantee
We refund you if you fail your exam.
Over 30 million students worldwide already upgrade their learning with Vaia!
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
LinkedList
In Java programming, a LinkedList is a dynamic data structure that allows you to store and manage a sequence of elements. Unlike arrays, LinkedLists can grow and shrink as needed. This makes them especially useful when you want to store data without worrying about capacity. Unlike an array, which has a fixed size, a LinkedList consists of nodes. Each node contains data and a reference to the next node in the sequence.
To use a LinkedList in Java, you need to import it from the `java.util` package. Here is a simple way to create a LinkedList to store first names:
To use a LinkedList in Java, you need to import it from the `java.util` package. Here is a simple way to create a LinkedList to store first names:
-
```java
LinkedList
namesList = new LinkedList (); ```
User Input
Handling user input is a crucial aspect of any interactive Java program. To receive input from users, a `Scanner` object can be employed. This object reads the data entered by the user during the runtime of the program. Here's how you can set it up:
- ```java Scanner scanner = new Scanner(System.in); ```
- ```java while(true) { System.out.print("Enter a first name or type 'exit': "); String name = scanner.nextLine(); if(name.equals("exit")) break; // process name } ```
Duplicate Checking
When adding elements to a collection like a LinkedList, often you will want to avoid adding the same item more than once. In our program, we accomplish this by checking for duplicates. Fortunately, the LinkedList class provides a handy method called `contains()`, which checks whether a certain element exists in the list.
Before adding a new name, simply verify its uniqueness:
Before adding a new name, simply verify its uniqueness:
- ```java if(!namesList.contains(name)) { namesList.add(name); } ```
Search Functionality
After storing the list of first names, you might want to search for a specific name within this list. Java's LinkedList makes this task straightforward with the `contains()` method, which facilitates checking whether the list includes a particular value. For example, to search for a name entered by the user:
- ```java System.out.print("Enter a name to search: "); String searchName = scanner.nextLine(); if(namesList.contains(searchName)) { System.out.println(searchName + " is in the list."); } else { System.out.println(searchName + " is not in the list."); } ```