2014-09-01 25 views
0

對於使用客戶端驗證和消息對話框彈出框爲空的文本框,您如何爲integerdouble編碼?Java客戶端/服務器驗證:如果jtextfield爲空?

我實現了String empname但是我似乎無法彈出一個對話框empidpayrate,任何建議將不勝感激。

String empname = jtfEname.getText(); 
    //If no text is entered display pop up dialog box. 
if(empname.length()==0) 
    JOptionPane.showMessageDialog(null, "Please Enter Your Name"); 

int empid = Integer.parseInt(jtfEid.getText().trim()); 
    //If no id is entered display pop up dialog box. 
if (empid.equals("")) 
    JOptionPane.showMessageDialog(null, "Please Enter Your Id"); 

double payrate = Double.parseDouble(jtfPayRate.getText().trim()); 
    //If no payrate is entered display pop up dialog box 
if (payrate.equals("")) 
    JOptionPane.showMessageDialog(null, "Please Enter Your Pay Rate"); 

回答

0

嘗試

int empid; 
try { 
    empid = Integer.parseInt(jtfEid.getText().trim()); 
} catch(NumberFormatException nfe) { 
    JOptionPane.showMessageDialog(null, "Please Enter Your Id"); 
} 

如果用戶輸入什麼,但int,這應該打開的對話框。 和payrate一樣。

double payrate; 
try { 
    payrate = Double.parseDouble(jtfPayRate.getText().trim()); 
} catch (NumberFormatException nfe) { 
    JOptionPane.showMessageDialog(null, "Please Enter Your Pay Rate"); 
} 
+0

現貨做得很好感謝您的意見,祝您有個美好的一天 – CaseyT 2014-09-01 09:16:50

+0

@CaseyT沒問題。樂於幫助。 :) – lxcky 2014-09-01 09:18:07

相關問題