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

In summary, the search method takes in a string parameter and uses a linked list to search for a matching last name. It compares the input to the last name of each node and returns a boolean value indicating whether the last name was found or not. A debugger can be used to inspect the values of the variables and ensure proper execution. Additionally, to search the last node, the while loop can be modified to check for both the current node and the found status.
  • #1
genu
22
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
  • #2
Use a debugger. Set a breakpoint on that line and inspect what the values for person.lastName and lastNameSearch are.
 
  • #3
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;
	}
 
  • #4
Just do "while(current != null)" and move the assignment to "person" between the while and the if.
 
  • #5
great..thx. That solved it >)
 

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

1. What makes a while loop confusing in Java?

A while loop can be confusing in Java because it repeats a block of code until a certain condition is no longer true. This can be difficult to keep track of, especially if the condition is complex or if there are nested loops.

2. How is a while loop different from a for loop in Java?

While loops and for loops are similar in that they both repeat a block of code, but they have different syntax and purposes. A while loop is better for situations where you don't know how many times the loop will run, while a for loop is better for a fixed number of iterations.

3. Can a while loop cause an infinite loop in Java?

Yes, if the condition in a while loop is never met, it will continue to repeat infinitely. This can happen if the condition is not properly defined or if the code inside the loop does not change the condition.

4. How can I avoid an infinite loop while using a while loop in Java?

To avoid an infinite loop, make sure that the condition in the while loop is properly defined and that the code inside the loop modifies the condition in some way. You can also use a break statement to exit the loop if a certain condition is met.

5. Can I use a while loop in Java to iterate through a collection?

Yes, you can use a while loop to iterate through a collection by using a counter variable and the size() method to determine the number of elements in the collection. However, using a for loop or a for-each loop is often a better and more efficient option for iterating through collections in Java.

Similar threads

  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
Back
Top