• 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

Alternative to SWITCH statement

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys!

the application that i am currently working on has a new "variable" to be added (so to speak) that requires me to relate it another existing variable, but the problem lies in the fact that the new variable will a different value for every different value of the existing variable and the existing variable has 400+ (!) different values. is there any alternative way to handle this other than create an obscenely long SWITCH statement?

thanks...
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure there are better ways.... could you give a better description on what type of variable you are comparing and what some of the possible values are? There are different methods depending on if your comparing a primitive, a String, or some custom object.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Kansh TR",

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
Kansh Sahasrabuddhe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Legg wrote:Sure there are better ways.... could you give a better description on what type of variable you are comparing and what some of the possible values are? There are different methods depending on if your comparing a primitive, a String, or some custom object.



hey Brian:

the reference variable is called "branch" which is an int type and the variable "old" has different values for each value of "branch". now the user has implemented a new standard and therefore the application has to be updated with the variable "new" for future use, but at the same time the all the values of "old" have to be transferred to "new" according 400+ different values of "branch" as there is no equation to convert "old" to "new".

old and new are both float type.

thanks
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like something for a Map. If old1|->new1 and old2|->new2 etc, that suggests a Map<Old, New> might be useful.
I am sure you know about Maps but here is the API link.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Campbell if I understand the problem correctly. Just throw em all in a Map and then pull out the "new" values based on the "old" key.
 
Kansh Sahasrabuddhe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Sounds like something for a Map. If old1|->new1 and old2|->new2 etc, that suggests a Map<Old, New> might be useful.
I am sure you know about Maps but here is the API link.



Brian Legg wrote:I agree with Campbell if I understand the problem correctly. Just throw em all in a Map and then pull out the "new" values based on the "old" key.



i think according to what you suggest, it should be Map<branch, difference> would be helpful, because there is no way to know the new values based on the old one. i only know the difference between old and new based on "branch"

thanks

 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have some way to work out the "new" values from the "old" values, you might be able to put some sort of algorithm in which makes a Map redundant.
 
reply
    Bookmark Topic Watch Topic
  • New Topic