Empty String validation for Multiple JTextfield

后端 未结 4 1401
说谎
说谎 2020-12-19 08:02

Is there a way to validate a number of JTextfields in java without the if else structure. I have a set of 13 fields, i want an error message when no entry is given for any o

4条回答
  •  一生所求
    2020-12-19 08:42

    Here is one way to do it:

    public static boolean areAllNotEmpty(String... texts)
    {
        for(String s : texts) if(s == null || "".equals(s)) return false;
        return true;
    }
    
    // ...
    
    if(areAllNotEmpty(firstName, lastName, emailAddress, phone))
    {
        JOptionPane.showMessageDialog(null, "No data entered");
    }
    

提交回复
热议问题