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 >)
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

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