Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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:
  • ```java LinkedList namesList = new LinkedList(); ```
By leveraging methods like `add()` and `contains()`, you can easily modify and access the data stored in your 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); ```
Using a loop, you can continually prompt the user to enter first names until they decide no more input is needed (e.g., by typing 'exit'). For example:
  • ```java while(true) { System.out.print("Enter a first name or type 'exit': "); String name = scanner.nextLine(); if(name.equals("exit")) break; // process name } ```
This allows for interactive and dynamic input into the LinkedList.
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:
  • ```java if(!namesList.contains(name)) { namesList.add(name); } ```
Using `contains()` helps to maintain a list filled only with unique elements, ensuring efficient memory usage and optimizing search operations.
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."); } ```
This approach allows users to interact with the list efficiently and confirms whether their query was found or not. It's a simple yet powerful way to use LinkedList.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free