I'm trying make a currency exchange program and I'm using a ComboBox to list the type of conversions the user wants to make and when the user selects a choice, it will make the conversion. Is there a better way to use the equals.IgnoreCase than using a bunch of if else statements? I created an array of strings for the choices, then declared the JComboBox with the array as the parameter.
@Override
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
double c = Double.parseDouble(text);
if(e.getSource()==comboBox) {
String choice = comboBox.getSelectedItem().toString();
if(choice.equalsIgnoreCase("Dollars to Pesos")) { // placeholder text
System.out.println(choice);
}
else if(choice.equalsIgnoreCase("Dollars to Pesos")) {
System.out.println(choice);
}
else if(choice.equalsIgnoreCase("Pesos to Dollars")) {
System.out.println(choice);
}
else if(choice.equalsIgnoreCase("Dollars to GB Pounds")) {
System.out.println(choice);
}
else if(choice.equalsIgnoreCase("GB Pounds to Dollars")) {
System.out.println(choice);
}
else if(choice.equalsIgnoreCase("Dollars to Yen")) {
System.out.println(choice);
}
else if(choice.equalsIgnoreCase("Yen to Dollars")) {
System.out.println(choice);
}
}
}
I created an array of strings for the choices Yes. Use that array. Assuming it's called
possibleChoices, that might look like