• Post Reply Bookmark Topic Watch Topic
  • New Topic

Search to avoid duplicates  RSS feed

 
Jack Horn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wondered if there is anyone here that can help me with this problem. I wish to search an array to avoid an item being entered if it has already been inserted. My code is:



The problem I seem to be having is that even when the item is found in the array it continues on and enters it in regardless. I have a feeling that there is something simple here that I am missing. So a sample output is:

Any ideas?
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jack, Welcome to JavaRanch.com. Check out the Collections Framework when you get a chance. You might find it easier to use than arrays.

http://java.sun.com/developer/onlineTraining/collections/Collection.html
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 24. You've got a rogue ; at the end.

Frank's right about the collections, though.
 
Jack Horn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Line 24. You've got a rogue ; at the end.

Frank's right about the collections, though.


Head in hands moment here! Cheers.

Will look into the collections
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also on the subject of line 24:

Might I suggest you replace that by an ordinary boolean operation instead of by that comparison to false?

Why? Two reasons: first, what I posted there says "If not stopFound" which is more straightforward to understand compared to a comparison to false. Second, one of these days your finger will slip and you will type this:

Here you are assigning false to the stopFound variable and seeing what the value of the result is. The result will always be false, regardless of what the original value of stopFound was. And the compiler won't give you any hint of your problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!