• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Search to avoid duplicates

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic