Looks good so far. You can use a do-while loop to cut out a bit of code.
Does that work? Am I correct in assuming you just need to add the duplication check?
Main Topics
Browse All TopicsHello. I need help writing 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.
This is what i have so far. Thank you.
import java.io.*;
import java.util.ListIterator;
import java.util.LinkedList;
public class llist
{
public static void main(String[] args)throws IOException
{
BufferedReader dataIn=new BufferedReader(new InputStreamReader(System.i
//create LinkedList object
LinkedList lList = new LinkedList();
String names="";
String response="";
System.out.print("");
System.out.print("Input a name:");
names = dataIn.readLine();
System.out.print("Would you like to enter another name? Enter yes or press any key to exit.");
response = dataIn.readLine();
if (response != null)
{
response = response.toUpperCase();
}
while (response != null && response.equals("YES"))
{
//add elements to LinkedList
lList.add(names);
System.out.print("Input name:");
names = dataIn.readLine();
System.out.print("Would you like to enter another name? Enter yes or press any key to exit.");
response = dataIn.readLine();
if (response != null)
{
response = response.toUpperCase();
}
}
lList.add(names);
ListIterator itr = lList.listIterator();
System.out.println("");
System.out.println("This is the list of names entered:");
while(itr.hasNext())
{
System.out.println(itr.nex
}
System.out.println("");
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You can use
while (response != null && response.equalsIgnoreCase(
although it's perhaps slightly clunky to keep requiring YES. Ask them to enter 0 when they've finished or something.
To avoid duplicates, use
if (!IList.contains(names)) {
IList.add(names);
}
although 'name' would be a better name here. btw, java variable names should begin lower case. Something like the following would be better:
if (!names.contains(name)) {
names.add(name);
}
alepru: As suggested above, in my earlier post, you can use HashSet (from Java Collection Framework) to store the values first (you can convert into lowercase if the you don't want case-insensitive duplicacy).
Being the subset of Set class (interface), one of the HashSet property is to remove the duplicate elements. This process of removing the duplicacy happens as and when you are adding elements into the Hashset.
Let me know if using HashSet is not an option
>>How do i do the search method and its still allowing duplicate names. thanks
Did you use the code i gave you at http:#25737263 ?
To search, use
int index = names.indexOf("foo");
if (index > -1) {
// found!
}
import java.io.*;
import java.util.ListIterator;
import java.util.LinkedList;
public class llist
{
public static void main(String[] args)throws IOException
{
BufferedReader dataIn=new BufferedReader(new InputStreamReader(System.i
//create LinkedList object
LinkedList lList = new LinkedList();
String names="";
String response="";
System.out.print("");
System.out.print("Input a name:");
names = dataIn.readLine();
System.out.print("Would you like to enter another name? Enter yes or press any key to exit.");
response = dataIn.readLine();
if (response != null)
{
response = response.toUpperCase();
}
while (response != null && response.equals("YES"))
{
System.out.print("Input name:");
names = dataIn.readLine();
System.out.print("Would you like to enter another name? Enter yes or press any key to exit.");
response = dataIn.readLine();
if (response != null)
{
response = response.toUpperCase();
}
}
if (lList.contains(names))
{
lList.add(names);
}
int index = names.indexOf("Aleona, Alex, Emilie, Johnny");
if (index > -1)
{
// found!
}
System.out.println("This is the list of names entered:");
System.out.println("");
lList.add(names);
ListIterator itr = lList.listIterator();
while(itr.hasNext())
{
System.out.println(itr.nex
}
System.out.println("");
}
}
while (response != null && response.equals("YES"))
{
System.out.print("Input name:");
names = dataIn.readLine();
if (lList.contains(names)==fa
{
lList.add(names);
}
System.out.print("Would you like to enter another name? Enter yes or press any key to exit.");
response = dataIn.readLine();
if (response != null)
{
response = response.toUpperCase();
}
}
Business Accounts
Answer for Membership
by: a_bPosted on 2009-11-03 at 22:10:41ID: 25736825
This section looks a little suspect --
while (response != null && response.equals("YES"))
{
//add elements to LinkedList
lList.add(names); // You are adding before taking the input --Not required here
System.out.print("Input name:");
names = dataIn.readLine();
System.out.print("Would you like to enter another name? Enter yes or press any key to exit.");
response = dataIn.readLine();
if (response != null)
{
response = response.toUpperCase();
}
}