How to write the below code in a simple way in java

how can i cut down the below logic in a simple way is there any alterantive
i dont want to write these many lines to verify the input and set the value.

 if(row[2] != null) {
                char frequencyVal = (Character) row[2];
                if(frequencyVal == Constants.HOURLY) {
                    dcfcMasterDTO.setFrequency(Util.castToString((frequency.get(Constants.A))));
                } else if(frequencyVal == Constants.DAILY) {
                    dcfcMasterDTO.setFrequency(Util.castToString((frequency.get(Constants.D))));
                } else if(frequencyVal == Constants.WEEKLY) {
                    dcfcMasterDTO.setFrequency(Util.castToString((frequency.get(Constants.W))));
                } else if(frequencyVal == Constants.MONTHLY) {
                    dcfcMasterDTO.setFrequency(Util.castToString((frequency.get(Constants.M))));
                } else if(frequencyVal == Constants.QUARTERLY) {
                    dcfcMasterDTO.setFrequency(Util.castToString((frequency.get(Constants.Q))));
                } else if(frequencyVal == Constants.HALFYEARLY) {
                    dcfcMasterDTO.setFrequency(Util.castToString((frequency.get(Constants.H))));
                } else {
                    dcfcMasterDTO.setFrequency(Util.castToString((frequency.get(Constants.Y))));
                }
            }

Open in new window

LVL 2
srikoteshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

srikoteshAuthor Commented:
How do I refactor this if else if logic into something more managable?
0
Amitkumar PSr. ConsultantCommented:
You can write something like below that is almost similar to the one you have written.


            if(row[2] != null) {
                char frequencyVal = (Character) row[2];
                String contants = null; // Assuming constants are of String
                if(frequencyVal == Constants.HOURLY) {
                    contants = Constants.A;
                } else if(frequencyVal == Constants.DAILY) {
                    contants = Constants.D;
                } else if(frequencyVal == Constants.WEEKLY) {
                    contants = Constants.W;
                } else if(frequencyVal == Constants.MONTHLY) {
                    contants = Constants.M;
                } else if(frequencyVal == Constants.QUARTERLY) {
                    contants = Constants.Q;
                } else if(frequencyVal == Constants.HALFYEARLY) {
                    contants = Constants.H;
                } else {
                    contants = Constants.Y;
                }
               
                dcfcMasterDTO.setFrequency(Util.castToString((frequency.get(constants))));
            }
0
gheistCommented:
switch ?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

ozoCommented:
Map ?
0
srikoteshAuthor Commented:
what i am trying here is i dont want to write those many else if logic
in future it may go 20 else ifs my code doesnt good to see .
i want to cut down these many steps.

hi ghiest,
even switch also the same only write i have write many steps.

hi ozo,
can u provide sample example

i have tried with like by taking all my constant into
char[] frequency ={'A''H''Y'}
AND ITERATED WITH FOR EACH LOOP
THIS MAY WORKS I HAVE TO TRY THIS OPTION
0
ozoCommented:
Were is Constants defined?
0
srikoteshAuthor Commented:
I HAVE TOTAL 6 CONSTANTS ARE THERE
A="Hourly"
D="Daily"
W="Weekly"
M="Monthly"
Q="Quarterly"
H="Halfyearly"
Y="Yearly"
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
What is your "frequency" object in the code above? The .get() method call makes me think that it might be a map, and in which case I wonder why you just can't populate the map with the Character values that you get in "frequencyVal". It looks as though you are doubling the mapping, ie. from Char (frequencyVal) to a String (in your Constants) and then again from those Constant strings to the final value from "frequency".
0
srikoteshAuthor Commented:
hi mccarl,

frequency object contains A,D,W,M,Q,H,Y  
Let suppose i am running one job (appln)everyday
how frequently i am doing this job  this may be weekly monthly daily,

from the DATA BASE if i get M means
in ui end i have to show monthly

so if u see the above code row[2] ,i am getting one value it may be any one of above 6 constants
i am verifying  with if else if logic and my condition satisfied i am setting the by getting it from map.
my code doesn't looks good, why because i have written to many else if logics,
i am looking any other alternatives.

i am getting these values from map<String,String> this is already used code so that's why i have tried to get the values,
instead of getting from there i have to do modification for my map<char,String>
0
ozoCommented:
private static final Map<String, String> constMap;
    static
{
    constMap = new HashMap<String, String>();
    constMap.put(Constants.HOURLY, Constants.A);
    constMap.put(Constants.DAILY, Constants.D);
    constMap.put(Constants.WEEKLY, Constants.W);
    constMap.put(Constants.MONTHLY, Constants.M);
    constMap.put(Constants.QUARTERLY, Constants.Q);
    constMap.put(Constants.HALFYEARLY, Constants.H);
}


dcfcMasterDTO.setFrequency(Util.castToString((frequency.get( constMap.getOrDefault(frequencyVal,Constants.Y) ))));
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikoteshAuthor Commented:
yes,this will works i will try it  and let u know
0
srikoteshAuthor Commented:
Excellent
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.