mercredi 30 septembre 2015

Java Web: Currency Converter [duplicate]

This question already has an answer here:

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!




Aucun commentaire:

Enregistrer un commentaire