【问题标题】:Java client/server validation: if jtextfield empty?Java 客户端/服务器验证:如果 jtextfield 为空?
【发布时间】:2014-09-01 08:24:09
【问题描述】:

对于使用带有消息对话框弹出框的客户端验证为空的文本字段,您如何为integerdouble 编写代码?

我实现了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");

【问题讨论】:

    标签: java if-statement dialog int messagedialog


    【解决方案1】:

    试试

    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");
    }
    

    【讨论】:

    • 发现做得好,感谢您的意见,祝您有美好的一天
    • @CaseyT 没问题。乐意效劳。 :)
    猜你喜欢
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 2020-09-08
    • 1970-01-01
    • 2017-01-26
    • 2010-09-14
    相关资源
    最近更新 更多