0

I have a problem with my If statement. The code runs without any problems, but it does not show the outputs for my if statements. It runs through all my JOptionboxes all the way to the end. Until it goes through my if statements.

import javax.swing.JOptionPane;

public class CurrencyConversion {

public static void main(String[] args) {

int x = 0;
double result,result1, result2;
String THAI = "THAI", EURO ="EURO", JPY = "JPY";

String name = JOptionPane.showInputDialog("What is your name: ");
String message = String.format("Welcome %s, to the Currency Exchange Program ", name); /*inputs name in %s*/ 
JOptionPane.showMessageDialog(null,  message); //displays the String format message
String currency = JOptionPane.showInputDialog("Which currency do you wish to exchange: "+THAI+ ", "+EURO+ ", "+JPY);
x = Integer.parseInt (JOptionPane.showInputDialog("Insert US Dollar Amount: "));
result = x * 32.57; //US TO THAI
result1 = x * .86; //US TO EURO
result2 = x * 117.50; //US TO JPY


if (currency == "THAI"){
    JOptionPane.showMessageDialog(null,"US Dollar Amount of "+x+ " dollars to be converted to "+THAI+" is: ");
    JOptionPane.showMessageDialog(null,result+ " BAHT");
}if (currency == "EURO"){
        JOptionPane.showMessageDialog(null,"The Amount of "+x+ " dollars to be converted to "+EURO+" is: ");
        JOptionPane.showMessageDialog(null,result1+ " EURO");
}if (currency == "JPY"){
            JOptionPane.showMessageDialog(null,"The Amount of "+x+ " dollars to be converted to "+JPY+" is: ");
            JOptionPane.showMessageDialog(null,result2+ " JPY");
}//end THAI if


}//end main
Ascalonian
  • 14,409
  • 18
  • 71
  • 103

2 Answers2

0

To compare Strings you have to use compareTo() or equals(), which are defined by Java.

currency.equals("THAI") is the same that currency.compareTo("THAI")==0

vls1
  • 39
  • 1
  • 7
0

You should use equals method to compare two strings...because == operator always compares whether the two objects are same or not where as equals method compares each character...

ssood9
  • 51
  • 3