I am trying to make a contains method where it will search for a name using the compareTo method of the Comparable interface. Here is my problem, It will return true if found but will stay in the while loop and not end if it is not found. AND I CANT FIGURE IT OUT!!!! ive been looking at it for hours and I need help lol

public boolean contains(Celebrities target) {
       LinearNode<T> current = contents;
       boolean result = false;

	while (result == false || current.getNext() != null) {
		if (target.compareTo(current.getElement()) == 0)
			result = true;

		else {
			result = false;
			current = current.getNext();
			}
		}

		return result;

	}
		}

The only way that loop terminates is if result = true and current.getNext() = null.

What you want is the binary AND logical operation not the binary OR logical operation.
Also there is no need for line 10. result will stay false unless it is true, and if it is true then the target is found and you can break.

The result in the while loop is really not needed, you can simply use a break statement like so :

public boolean contains(Celebrities target){
 LinearNode<T> start = contents;
 boolean isFound = false;
 while(start.getNext() != null){
   if(_equal(target,start)){ 
       isFound = true; break;
    }
  start.moveForward();
 }
 return isFound;
 }
}

Ok I adapted it and finally got it correct...Im really not that dumb I just changed around so many things at different times and forgot what I changed so I was making little fixes as I went and either got errors or not what I wanted.

But I got it! Thanks for the help!!!

Working method:

public boolean contains(Celebrities target){
		LinearNode<T> current = contents;
		boolean result = false;

		while(result == false && current != null){
			if(target.compareTo(current.getElement()) == 0)
				result = true;

			else {
				
				current = current.getNext();
			}	
		}


		return result;


	}

Good Job.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.