0

I'm working on a project due for one of my classes. In it, we need to create a currency converter that converts one currency to another using an array of conversion rates. However, I can't get my program to get a conversion rate from an 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 the array and my method of getting the number from the array. I'm getting the error that Currency1 and Currency2 might not have been initialized even though the if statements clearly initialize them. Can anyone see what I'm doing wrong?

Also, I'm sure there's a better way to set the values of Currency1 and 2 than 8 different if statements but I just want the program to work before I worry about efficiency. If you want to help me with that too I'd appreciate it but it's not a priority.

Thank you!

Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
DrMoney
  • 73
  • 8
  • Ignoring for a second that you're comparing strings incorrectly, what if `inCurr` had a value of `"JPN"`? What's the value of `Currency1` then? – Sotirios Delimanolis Sep 30 '15 at 18:56
  • Read this: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Sotirios Delimanolis Sep 30 '15 at 18:58
  • @SotiriosDelimanolis it's a drop down box, the user can't enter their own currency. They can only use one of the 4 currencies in the drop down. I also realized I was comparing strings incorrectly but when I switched them to Currency1.equals("CAD") it threw an exception while the == did not. – DrMoney Sep 30 '15 at 19:01
  • Why do you expect the compiler to know that those are the only possible values at the moment? – Sotirios Delimanolis Sep 30 '15 at 19:02
  • Why do you expect `Currency1`, which is a primitive variable to have an `equals` method? What would invoking such a method even do? – Sotirios Delimanolis Sep 30 '15 at 19:03

1 Answers1

1

change these lines

int Currency1;
int Currency2;

to

int Currency1 = 0;
int Currency2 = 0;

change the comparisons that are like

outCurr == "USD"

to be more like

"USD".equals(outCurr)
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36