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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

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