How do I search a linked list for a string ? findXX(string) , for instance.

Recommended Answers

Do you know how to write a loop that traverses the list from the first node to the last?

Jump to Post

Yes, so you loop through the list checking the value for each node to see it it's the one you're looking for.

Jump to Post

All 5 Replies

Do you know how to write a loop that traverses the list from the first node to the last?

Can't edit the post....

Or could I not ask the user what book he's looking for and then....

if (NameList1.contains("JName of Book")) {
    // code to execute if book is in the list
}

About the traversing..

ref = ref.next;

Node ref = mylist;
while (ref != null)
{
System.out.println(ref.value);
ref = ref.next
}

That's what the book gives me. SOmething like that ?+

Yes, so you loop through the list checking the value for each node to see it it's the one you're looking for.

How do I exactly do this, though.

//get user input 
    input = JoptionPane.showInputDialog("Enter the name of the book:");

    variable = String.parseString(variable);

    if (LinkedList.contains("Name of Book")) {
    JOptionPane.showMessageDialog(null, "Result ");
    }

Is LinkedList.contains correct? I need a variable, I guess. How do I do this here with a linked list?

If you are using the Java API LinkedList class then yes, but if you are writing your own LinkedList class then you have to use a loop to search all the nodes for the value you want

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.