wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Alternative to SWITCH statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Alternative to SWITCH statement" Watch "Alternative to SWITCH statement" New topic
Author

Alternative to SWITCH statement

Kansh Sahasrabuddhe
Greenhorn

Joined: Apr 07, 2009
Posts: 5
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

Joined: Nov 07, 2008
Posts: 488
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.


SCJA
~Currently preparing for SCJP6
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

"Kansh TR",

Please read your private messages regarding an important announcement.

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kansh Sahasrabuddhe
Greenhorn

Joined: Apr 07, 2009
Posts: 5
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

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Nov 07, 2008
Posts: 488
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

Joined: Apr 07, 2009
Posts: 5
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

Joined: Oct 13, 2005
Posts: 38007
    
  22
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.
 
 
subject: Alternative to SWITCH statement
 
Similar Threads
Any scenario where throwing an AssertionError directly is acceptable?
compile time constant and final variable
New here; please help(",): Play Shape; A program that reads a shape number!?!?!?!?
questions about assert
Java histogram program.. bit of help required please?