0

I am currently working on a simple java project that converts a selected currency, to a selected currency. I seem to be having a problem with the if/elseif statements because whenever I run the method to do the appropriate maths, the original variable stays the same. I originally tried to run a boolean operation with a string entered by the user("USD", "EUR") via the .equals method but I ran into the same issue.

Here is a portion of my method where I think the error arises(whole method for clarity added**):

        public double doMath(double USD, double CAD, double GBP, double YEN, double EUR, double CHF, int ogCurrency, int newCurrency, double howMuch){
    if (ogCurrency == 2){
        if (newCurrency == 1){
            howMuch = howMuch * CAD;
        }
        else if (newCurrency == 3){
            howMuch = howMuch * GBP;
        }
        else if (newCurrency == 6){
            howMuch = howMuch * YEN;
        }
        else if (newCurrency == 4){
            howMuch = howMuch * EUR;
        }
        else if (newCurrency == 5){
            howMuch = howMuch * CHF;
        }
    }
    else if (ogCurrency == 1){
        howMuch = howMuch / CAD;
        if (newCurrency == 2){
            howMuch = howMuch * USD;
        }
        else if (newCurrency == 3){
            howMuch = howMuch * GBP;
        }
        else if (newCurrency == 6){
            howMuch = howMuch * YEN;
        }
        else if (newCurrency == 4){
            howMuch = howMuch * EUR;
        }
        else if (newCurrency == 5){
            howMuch = howMuch * CHF;
        }
    }
    else if (ogCurrency == 3){
        howMuch = howMuch / GBP;
        if (newCurrency == 1){
            howMuch = howMuch * USD;
        }
        else if (newCurrency == 2){
            howMuch = howMuch * CAD;
        }
        else if (newCurrency == 6){
            howMuch = howMuch * YEN;
        }
        else if (newCurrency == 4){
            howMuch = howMuch * EUR;
        }
        else if (newCurrency == 5){
            howMuch = howMuch * CHF;
        }
    }
    else if (ogCurrency == 6){
        howMuch = howMuch / YEN;
        if (newCurrency == 1){
            howMuch = howMuch * USD;
        }
        else if (newCurrency == 3){
            howMuch = howMuch * GBP;
        }
        else if (newCurrency == 2){
            howMuch = howMuch * CAD;
        }
        else if (newCurrency == 4){
            howMuch = howMuch * EUR;
        }
        else if (newCurrency == 5){
            howMuch = howMuch * CHF;
        }
    }
    else if (ogCurrency == 4){
        howMuch = howMuch / EUR;
        if (newCurrency == 1){
            howMuch = howMuch * USD;
        }
        else if (newCurrency == 3){
            howMuch = howMuch * GBP;
        }
        else if (newCurrency == 6){
            howMuch = howMuch * YEN;
        }
        else if (newCurrency == 2){
            howMuch = howMuch * CAD;
        }
        else if (newCurrency == 5){
            howMuch = howMuch * CHF;
        }
    }
    else if (ogCurrency == 5){
        howMuch = howMuch / CHF;
        if (newCurrency == 1){
            howMuch = howMuch * USD;
        }
        else if (newCurrency == 3){
            howMuch = howMuch * GBP;
        }
        else if (newCurrency == 6){
            howMuch = howMuch * YEN;
        }
        else if (newCurrency == 4){
            howMuch = howMuch * EUR;
        }
        else if (newCurrency == 2){
            howMuch = howMuch * CAD;
        }
    }
    /*else{
        System.out.println("Something didn't add up");
    }*/
    return howMuch;  

&& main if that will help

    public static void main(String[] args) {
        double USD = 1.00;
        double CAD = 1.31; // canadian dollar
        double YEN = 101.20; // jap yen
        double GBP = 0.77; // british pound
        double EUR = 0.89; // euro
        double CHF = 0.97; // swiss franc
    // initiate object of class
    CurrencyConverter converterBasic;

    // assigning default values to fractionsBasic
    converterBasic = new CurrencyConverter(1, 1, 1.00);


    // new scanner for fetching input, premade class Scanner creating object in to run method(parameters/gets input from System.in)

    Scanner in = new Scanner(System.in);


    System.out.print("Enter your starting currency #(1: CAD, 2: USD, 3: GBP, 4: EUR, 5: CHF, 6: YEN): ");
    int ogCurrency;
    ogCurrency = in.nextInt();
    converterBasic.setOgCurrency(ogCurrency);


    System.out.print("Enter your desired currency please #(1: CAD, 2: USD, 3: GBP, 4: EUR, 5: CHF, 6: YEN): ");
    int newCurrency;
    newCurrency = in.nextInt();
    converterBasic.setNewCurrency(newCurrency);

    System.out.print("Enter how much cash you have please: ");
    double howMuch;
    howMuch = in.nextDouble();
    converterBasic.setHowMuch(howMuch);


    // use appropriate methods defined in class to compute conversion
    converterBasic.doMath(USD, CAD, GBP, YEN, EUR, CHF, ogCurrency, newCurrency, howMuch);

    // print appropriate output formatted using printf

    System.out.printf(converterBasic.toString());

Does anyone spot what the problem with this by chance, it's definitely right under my nose. Thanks so much in advance, I look forward to hearing your wisdom.

toliot
  • 13
  • 4
  • 2
    how this even compiled, there is no return statement in your doMath , you didn't add the complete code – Pavneet_Singh Oct 07 '16 at 12:43
  • @JynXXedRabbitFoot sorry, but that is simply wrong. Java is pure bass-by-value (["When the method or constructor is invoked (§15.12), **the values of the actual argument expressions initialize newly created parameter variables**, each of the declared type, before execution of the body of the method or constructor."](http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.1)). – Turing85 Oct 07 '16 at 13:28

3 Answers3

0

http://ideone.com/g0ECVT Is Java "pass-by-reference" or "pass-by-value"?

Java pass arguments by value.

public static void add(int a) {
    a+=2;
}

public static void main (String[] args) throws java.lang.Exception
{
    int a = 2;
    add(a);
    System.out.println(a);//says 2
}
Community
  • 1
  • 1
Emil Hotkowski
  • 2,233
  • 1
  • 13
  • 19
  • Ok but I am trying to check whether the original currency number (value) is equal to the list I completed for the if/elseif's (comparing to other value). so if the user will enter 1 for USD then the if statement currency == 1; should work, shouldn't it ? – toliot Oct 07 '16 at 12:49
  • The problem isn't your comparisons. The problem is that you are discarding the result of the `doMath()` method. You need to save the result in a variable. – puhlen Oct 07 '16 at 13:16
0

I would use an enum personally.

Try this.

import java.text.NumberFormat;
import java.util.Currency;
import java.util.Locale;
import java.util.Scanner;

public class Solution
{

  public static void main(String[] args)
  {
    ConversionFactor ogCurrency;
    ConversionFactor newCurrency;
    String currencies = " #(1: CAD, 2: USD, 3: GBP, 4: EUR, 5: CHF, 6: YEN): ";
    Scanner in = new Scanner(System.in);

    System.out.print("Enter your starting currency" + currencies);
    ogCurrency = ConversionFactor.values()[in.nextInt()];

    System.out.print("Enter your desired currency " + currencies);
    newCurrency = ConversionFactor.values()[in.nextInt()];

    System.out.print("Enter how much cash you have please: ");
    double onHand;
    onHand = in.nextDouble();
    in.close();
    onHand = onHand / ogCurrency.value;
    onHand = onHand * newCurrency.value;

    Currency currentCurrency = Currency.getInstance(Locale.getDefault());
    NumberFormat currencyFormatter = NumberFormat
        .getCurrencyInstance(Locale.getDefault());

    System.out.println(Locale.getDefault().getDisplayName() + ", "
        + currentCurrency.getDisplayName() + ": "
        + currencyFormatter.format(onHand));
  }

  public enum ConversionFactor
  {
    CAD(1.31), USD(1.0), GBP(.77), EUR(.89), CHF(.97), YEN(101.2);
    ConversionFactor(double value)
    {
      this.value = value;
    }

    double value;
  }
}
JynXXedRabbitFoot
  • 996
  • 1
  • 7
  • 17
0

Your method assumes parameter is passed by reference, however Java passes by value.

Any operation on same object is valid and will change the original value but anything that will create new object will be local to the function and original value will remain unchanged.

void modify(Foo foo){
     foo.setValue("new value");//accepted and valid
     foo = new Foo("new Value");//will create a local object 
                                //and will not alter the original object
}

In your method howMuch = howMuch*a; creates new Object and hence is not referenced in original value.

So you can't manipulate a object of Immutable class inside method, as for immutable class changing its value requires creating new Object. All of the java.lang package wrapper classes are immutable: Boolean, Byte, Character, Double, Float, Integer, Long, Short, String source

So you can do something like this

double howMuch;
howMuch = in.nextDouble();
converterBasic.setHowMuch(howMuch);
converterBasic.setHowMuch(converterBasic.doMath(USD, CAD, GBP, YEN, EUR, CHF, ogCurrency, newCurrency, howMuch));

This will work as you expect it work, it will set the new value to howMuch variable of converterBasic Object.

Saurabh Singh
  • 381
  • 5
  • 15