0

I am trying to search through a Java LinkedList that uses a custom object called Name. I need to search on first name (My compareTo method in Name already compares last names because I need to use it to sort by last name). Name has an observer method called getFirstName().

I am not having any success in accessing first name from my LinkedList. This is what I want to do but this (obviously) doesn't work.

if (iterator.next().getFirstName().equals(inputSearch))

Can someone point me in the right direction?

This is the full method I am currently trying to write:

// Creating a method to search for a first name
 static void searchName()
{
 Scanner inData = new Scanner(System.in);
 // Label to request input from user
 System.out.println("Enter the first name that you would like to search for:");
 // Setting variable to capture input
 String inputSearch = inData.next();
 // Creating an iterator to search through the list
 iterator = list.iterator(); 
 // While loop to search each entry
 while (iterator.hasNext())
 {
 if (iterator.next().getFirstName().equals(inputSearch))
 {
 System.out.println("MATCH FOUND: " + iterator.next());
 }
 }
} 
asked Oct 22, 2012 at 22:50
3
  • 1
    What specifically doesn't work? I mean, clearly calling next() twice is... sub-optimal. Commented Oct 22, 2012 at 22:56
  • "if (iterator.next().getFirstName().equals(inputSearch))" gives an error: cannot find symbol - method getFirstName() Commented Oct 22, 2012 at 23:03
  • I definitely would be open to any ...optimal... solution Commented Oct 22, 2012 at 23:04

2 Answers 2

2

You're calling iterator.next() twice. The second time will advance past the item you want. Instead, save the return value from the first call to iterator.next() and use that.

 while (iterator.hasNext())
 {
 Name item = (Name) iterator.next();
 if (item.getFirstName().equals(inputSearch))
 {
 System.out.println("MATCH FOUND: " + item);
 }
 }

or, more idiomatically

 for (Name item : list)
 {
 if (item.getFirstName().equals...
 }
answered Oct 22, 2012 at 23:03
2
  • When I try to compile the modified code I get a compiler error "incompatible types - found java.lang.Object but expected Name" on Name item = iterator.next(); Commented Oct 22, 2012 at 23:20
  • I fixed the example. Your List is not generic, and should be declared as List<Name> rather than just List. If you change the declaration to List<Name> then you can omit the cast. Commented Oct 22, 2012 at 23:22
2
while (iterator.hasNext()) { 
 if (iterator.next().getFirstName().equals(inputSearch)) { //iterator.next() 
 System.out.println("MATCH FOUND: " + iterator.next()); //iterator.next() 
 } 
 } 

Since you are calling next() twice, while printing it would be next object.

try storing whatever iterator.next() returns in its corresponding type and use it to compare and print if succeed.

ex:

while(iterator.hasNext(){
 Name name=iterator.next();
 if(name.getFirstName().equals(inputSearch)){
 System.out.println("Match Found"+name);
 }
}

This is what I see wrong. Not aware of anything else.

answered Oct 22, 2012 at 23:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.