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

  • Context: Java 
  • Thread starter Thread starter genu
  • Start date Start date
  • Tags Tags
    Confusing Java Loop
Click For Summary

Discussion Overview

The discussion revolves around a Java method for searching last names within a linked list of ExtPerson objects. Participants explore issues related to the while loop's structure and its ability to search through all nodes, particularly the last node.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant presents a search method that fails to find last names in the last node of the linked list, noting that the condition is always false.
  • Another participant suggests using a debugger to inspect values of lastName and lastNameSearch to diagnose the issue.
  • A participant questions whether the search method needs to handle the last node separately or if it can be included in the loop.
  • Another participant proposes changing the loop condition to "while(current != null)" and suggests moving the assignment of the person variable to occur between the while and if statements.
  • A later reply indicates that the proposed change resolved the issue.

Areas of Agreement / Disagreement

Participants generally agree on the need to modify the loop condition to include the last node, with some differing on the specifics of implementation.

Contextual Notes

Limitations include potential assumptions about the structure of the linked list and the handling of null values, which may affect the search logic.

Who May Find This Useful

Java programmers, particularly those working with linked lists or searching algorithms, may find this discussion relevant.

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 >)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
15K
  • · Replies 2 ·
Replies
2
Views
4K