Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with some code.

 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone i hope someone can help me.
i want to be able to test 10 strings(some of the strings might have the same value) i want to be able test each string in turn with a loop though the hashtable. so for example if the hashtable equals string 1 extract the strings that do equal string 1 and put them inside a array list along with there key values, and then delete the ones that have been found to equal string 1, so that when i test again it will not find them again. then i want a loop to test the next string (string2) and do the same to that string and so on with the rest of the strings.
could someone please help me as my code does work but it is hard coded and i need some help please.
thanks for yr time.
ben
 
Norm Miller
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you could show us what you have done so far. Use UBB code tags to bracket the code.
 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what i have done so far it works how i want it to but it is just so ugly
thanks
ben

(edited by Cindy to close the code block - although now it is SOOOO wide . . . )
[ December 13, 2002: Message edited by: Cindy Glass ]
 
Greg Charles
Sheriff
Posts: 2993
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, you're right. That is ugly!
OK, I'll make a few suggestions. First of all, why a while(hash.containsValue()) ? I'd use, "if" instead. In fact, the unconditional break at the end of the loop makes it act like an "if" structure anyway. If you ever see an unconditional break or continue ... that is one not in an "if" structure ... you know you've made a mistake.
Here's another idea. How about putting the strings into an array, rather than individually named variables? Then you could use a loop and not have to cut and paste the same block of code ten times. If that approach doesn't work, how about breaking that duplicated block into a method that you call ten times. An identical or nearly identical block of code duplicated ten times is also a sure sign of a programming mistake.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is a different way of ( hopefully ) accomplishing some of what you tried to do with the Hashtable

Not sure what you need to do in your program, but something like a dual arraylist seems more practical to me. Usually it is obvious when you've chosen the wrong data structure ( or are misusing it ) and all hands are pointing to 'misuse'! There is little functionality in my code snippet and I just typed it off the top of my head, so don't assume it will compile! But it may give you some other ideas to make your life easier!
Jamie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic