MezyToke
MezyToke

Reputation: 7

Unsure how to calculate the variables stated

    double Euro = 1.37;
    double USAD = 1.81;
    double JapYen = 190.00;
    double Zloty = 5.88;

    //First menu choice screen
    if (MenuChoice == 1) {
        System.out.println("Which of the following currencies do you wish to exchange into sterling?");
        System.out.println("Euro - EUR");
        System.out.println("USA Dollar - USD");
        System.out.println("Japanese Yen - JPY");
        System.out.println("Polish Zloty - PLN");
        System.out.print("Please enter the three letter currency:   ");

        //Currency input validation
        String CurChoice = "";
        boolean isCorrectCurrency = false;
        do {
            CurChoice = keybStr.next();
            isCorrectCurrency = CurChoice.matches("^EUR|USD|JPY|PLN$");

            if (isCorrectCurrency) {
                System.out.println("");
            } else {
                System.out.print("Invalid Currency Entered. Please Retry: ");
            }

        } while (!isCorrectCurrency);

        //Exchange amount calculator
        System.out.print("Enter the amount you wish to exchange of " + CurChoice + ": ");
        double ExcAmount = keybStr.nextInt();

        if (CurChoice == "EUR") {
            double result = ExcAmount / Euro;
            System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
        } else if (CurChoice == "USD") {
            double result = ExcAmount / USAD;
            System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
        } else if (CurChoice == "JPY") {
            double result = ExcAmount / JapYen;
            System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
        } else if (CurChoice == "PLN") {
            double result = ExcAmount / Zloty;
            System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
        } else {
            System.out.println("");
        }

    }//if

So this is what I got so far. I want the code to say (as an example) "500.00 in EUR = £364.96" though when I run the code it just ends after the user inputs which currency they wish to use. Any help? I wasn't sure whether i needed to put the if statement within a loop or something.

Upvotes: 0

Views: 53

Answers (2)

auntyellow
auntyellow

Reputation: 2573

If you are using Java 1.7+, switch also works:

switch (CurChoice) {
case "EUR":
    double result = ExcAmount / Euro;
    System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
    break;
case "USD":
    double result = ExcAmount / USAD;
    System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
    break;
...
default:
    System.out.println("");
}

But the best approach is to set all currency code and exchange rate into a map, e.g.

HashMap<String, Double> = new HashMap<>();
curMap.put("EUR", Euro);
curMap.put("USD", USAD);
...
Double rate = curMap.get(CurChoice);
if (rate == null) {
    System.out.println("");
} else {
    double result = ExcAmount / Double.valueOf(rate);
    System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
}

Upvotes: 0

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21575

As mentioned in this answer using == for strings checks if they are the same item. In this case even though both CurChoice and one of the literal strings "EUR" may have the same value it will still be false as this is comparing references. So what's happening is they are all false and the system prints out a blank line from the else. To fix this use .equals() for your comparisons.

    if (CurChoice.equals("EUR")) {
        double result = ExcAmount / Euro;
        System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
    } else if (CurChoice.equals("USD")) {
        double result = ExcAmount / USAD;
        System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
    } else if (CurChoice.equals("JPY")) {
        double result = ExcAmount / JapYen;
        System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
    } else if (CurChoice.equals("PLN")) {
        double result = ExcAmount / Zloty;
        System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
    } else {
        System.out.println("");
    }

Upvotes: 1

Related Questions