Hi can anyone help me with my java program? I'm currently making a currency converter and i need to make an option where the user can enter a British pound amount and click convert for it to convert, then have the option to enter their own exchange rate into the calculation and when convert is clicked then it will use the users inputted exchange rate instead of the one that i have set. Here is the code i have so far to convert GBP to USD, the program works when i input an amount and also input an exchange rate myself, however the option to leave the input exchange rate box empty and use the constant exchange rate set brings an error when converted:
public class cConverter extends javax.swing.JFrame {
double GBPtoUSD = 1.288;
//this is the constant exchange rate for GBP to USD.
private void BtnConvertActionPerformed(java.awt.event.ActionEvent evt) {
double ConvertFromGBP = Double.parseDouble(InputFrom.getText());
double GetExchange = Double.parseDouble(ExchangeRateFrom.getText());
/* Input from is where the user inputs the GBP amount they want converted.
ExchangeRateFrom is the optional exchange rate box where the user inputs an
updated exchange rate if the constant one is out of date. */
if (CurrencyTop.getSelectedItem().equals("USD")){
String cGBPtoUSD = String.format("%.2f", ConvertFromGBP * GBPtoUSD);
ConvertedFrom.setText(cGBPtoUSD);
}
else if (CurrencyTop.getSelectedItem().equals("USD")) {
String uGBPtoUSD = String.format("%.2f", GetExchange * ConvertFromGBP);
ConvertedFrom.setText(uGBPtoUSD);
}
/* CurrencyTop is a combo box containing the currencies to convert to.
ConvertedFrom is the calculation output label. */
Error exception when converting without inputting an exchange rate (should be using the GBPtoUSD double when nothing is inputted to this box):
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String