Java Java While Loop for Searching Last Names: Solving Confusion in ExtPerson Class

AI Thread Summary
The discussion revolves around a Java method designed to search for a last name in a linked list of `ExtPerson` objects. The initial implementation fails to find matches in the last node because the loop condition only checks for `current.link != null`, which prevents the last node from being evaluated. A user suggests using a debugger to inspect the values of `person.lastName` and `lastNameSearch` to understand the issue better. The solution proposed involves changing the loop condition to `while(current != null)` and moving the assignment of `person` to occur within the loop, thus allowing the last node to be included in the search. This adjustment resolves the issue, enabling the method to successfully find last names in all nodes of the linked list.
genu
Messages
22
Reaction score
0
Code:
	public boolean search (String lastNameSearch) {
		LinkedListNode<T> current;
		
		current = first;
		boolean found = false;
		ExtPerson person = (ExtPerson) first.info;
		
		while (current.link != null) {
			
			if ((person.lastName.toLowerCase().equals(lastNameSearch.toLowerCase()))) {
				found = true;
				System.out.println("I FOUND IT"); // NEVER REACHED because the condition is always false
				break;
			} else {
				current = current.link;
				person = (ExtPerson) current.info;
			}
			System.out.println(person.lastName.toLowerCase().equals(lastNameSearch.toLowerCase())); //RETURNS "true"
		}
		
		return found;
	}

a little more specific:

http://i37.tinypic.com/2qsbfqt.png
 
Technology news on Phys.org
Use a debugger. Set a breakpoint on that line and inspect what the values for person.lastName and lastNameSearch are.
 
thanks, well it seems that it doesn't find items that are in the last node. How can I make it search the last node as well in this loop? or do I have to compare the input to the last node separately?

Code:
public boolean search (String lastNameSearch) {
		LinkedListNode<T> current;
		
		boolean found;
		current = first;
		found = false;
		ExtPerson person = (ExtPerson) first.info;
			
		while (current.link != null && !found) {
			if ((person.lastName.toLowerCase().equals(lastNameSearch.toLowerCase()))) {
				found = true;
			} else {
				current = current.link;
				person = (ExtPerson) current.info;
			}
			count++;
		}
		
		return found;
	}
 
Just do "while(current != null)" and move the assignment to "person" between the while and the if.
 
great..thx. That solved it >)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
3
Views
4K
Replies
3
Views
3K
Replies
1
Views
2K
Replies
5
Views
4K
Replies
12
Views
2K
Replies
6
Views
3K
Replies
1
Views
15K
Replies
2
Views
4K
Back
Top