• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alternative to SWITCH statement

 
Kansh Sahasrabuddhe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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...
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic