2017-05-14 1 views
0

안녕하세요 누구든지 내 Java 프로그램을 도와 줄 수 있습니까? 나는 현재 통화 변환기를 만들고 있는데 사용자가 영국 파운드 금액을 입력하고 변환을 위해 변환을 클릭 한 다음 계산에 자체 환율을 입력하는 옵션이 있고 변환이 클릭 될 때 옵션을 만들 필요가 있습니다. 그러면 사용자가 입력 한 환율 대신에 내가 입력 한 환율을 사용하게됩니다. 여기에 지금까지 GBP를 USD로 변환하는 코드가 있습니다. 프로그램은 금액을 입력하고 직접 환율을 입력 할 때 작동하지만 입력 환율 상자를 비워두고 상수 환율을 사용하는 옵션은 변환 오류 (어떤 것도이 박스에 입력되지 않을 때 두 배 GBPtoUSD를 사용한다) 환율 입력없이 변환Java Netbeans IDE 변환 오류가 발생합니다

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. */ 

오류 예외 : 글

예외 "AWT-EventQueue의-0" java.lang.NumberFormatException : empty String

답변

0

예외는 아마도 여기에서 발생합니다 :

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

당신은 (또한 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; 
    } 
} 

다음 하나의 경우 블록의 변환을 수행하기위한 GetExchange를 사용

if (CurrencyTop.getSelectedItem().equals("USD")){ 
    String uGBPtoUSD = String.format("%.2f", GetExchange * ConvertFromGBP); 
    ConvertedFrom.setText(uGBPtoUSD); 
} 
+0

답장을 보내 주셔서 감사합니다. xormar, 나는 당신이 나에게 준 코드를 구현했지만 여전히 같은 오류가 발생합니다. –

+0

InputFrom.getText()에서 빈 입력을 확인 했습니까? ExchangeRateFrom에서 어떤 현상이 발생하고 있습니까? 게시물에 전체 스택 트레이스와 업데이트 된 코드를 추가 할 수 있습니까? 아마 다른 곳에서 오류가 발생했을 수 있습니까? – xormar

+0

https://gist.github.com/liamrice18/2aae2ec72bdfa34ea308519d131c47b8 –