0

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

L.Rice
  • 1
  • 1

1 Answers1

0

Your exception probably happens here:

double GetExchange = Double.parseDouble(ExchangeRateFrom.getText());

You should check for empty input (also on the InputFrom.getText()):

String input = ExchangeRateFrom.getText();
if(null == input || input.isEmpty()) {
    GetExchange = GBPtoUSD;
}
else {
    try {
        GetExchange = Double.parseDouble(input);
    }
    catch(Exception ex) {
        // use default or do something else on invalid input
        GetExchange = GBPtoUSD; 
    }
}

and then use GetExchange for doing the conversion in one single if-block:

if (CurrencyTop.getSelectedItem().equals("USD")){
    String uGBPtoUSD = String.format("%.2f", GetExchange * ConvertFromGBP);
    ConvertedFrom.setText(uGBPtoUSD);
 }
xormar
  • 81
  • 1
  • 13
  • Thank you for the reply xormar, i have implemented the code you have given me however i'm still getting the same error. – L.Rice May 14 '17 at 17:53
  • Did you also check for empty input in InputFrom.getText()? What's happening in ExchangeRateFrom? Can you add the complete stacktrace and the updated code to your post? Maybe the error happens somewhere else? – xormar May 15 '17 at 07:02
  • https://gist.github.com/liamrice18/2aae2ec72bdfa34ea308519d131c47b8 – L.Rice May 15 '17 at 15:51
  • Try removing line 7 (` double GetExchange = Double.parseDouble(ExchangeRateFrom.getText();`) from your code above. – xormar May 21 '17 at 10:38