tomcat - Java Web: Currency Converter -
this question has answer here:
i'm working on project due 1 of classes. in it, need create currency converter converts 1 currency using array of conversion rates. however, can't program conversion rate array.
private static final double[][] rates = { {1.0, 0.624514066, 0.588714763, 0.810307 }, {1.601244959, 1.0, 0.942676548, 1.2975}, {1.698615463, 1.060809248, 1.0, 1.3764}, {1.234100162, 0.772200772, 0.726532984, 1.0} }; public double getfxrate(final string incurr, final string outcurr){ int currency1; int currency2; double rate; if(incurr == "cad"){ currency1 = 0; } if(incurr == "eur"){ currency1 = 1; } if(incurr == "gbp"){ currency1 = 2; } if(incurr == "usd"){ currency1 = 3; } if(outcurr == "cad"){ currency2 = 0; } if(outcurr == "eur"){ currency2 = 1; } if(outcurr == "gbp"){ currency2 = 2; } if(outcurr == "usd"){ currency2 = 3; } rate = rates[currency1][currency2]; return rate; } } you can see array , method of getting number array. i'm getting error currency1 , currency2 might not have been initialized though if statements initialize them. can see i'm doing wrong?
also, i'm sure there's better way set values of currency1 , 2 8 different if statements want program work before worry efficiency. if want me i'd appreciate it's not priority.
thank you!
change these lines
int currency1; int currency2; to
int currency1 = 0; int currency2 = 0; change comparisons like
outcurr == "usd" to more like
"usd".equals(outcurr)
Comments
Post a Comment