有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java:在存储到DDC之前验证属性

我正在尝试验证用户的输入。 如果用户输入firstName,程序将进行验证,以确保firstName在转到最后一个enter code here名称之前不为null或空字符串。 你能帮我解决这个问题吗? 导入javax。摆动JOptionPane

public class implementation{
    public static void main(String[] args)
    {
        //studentTesting student=new studentTesting("asd","asd");
        // getting first name and last name. Make sure each value is valid before moving to another attribute.
        boolean allValid=false;
        do {
            try {
                Student1 student=new Student1(JOptionPane.showInputDialog("Enter first name"),JOptionPane.showInputDialog("Enter lastname"));
                allValid=true;
            }catch(IllegalArgumentException e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        }while(!allValid);
    }
}
class Student1 {
    private String firstName;
    private String lastName;

    public Student1(String firstName, String lastName) {
        if(firstName==null || firstName.equals(""))
        {
            throw new IllegalArgumentException("first name Error");
        }
        if(lastName==null || lastName.equals(""))
        {
            throw new IllegalArgumentException("last name Error");
        }
        this.lastName=lastName;
        this.firstName=firstName;



    }
    public void setLastName(String lastName) {
        if(lastName==null || lastName.equals(""))
        {
            throw new IllegalArgumentException("last name Error");
        }
        this.lastName=lastName;
    }
    public void setFirstName(String firstName) {
        if(firstName==null || firstName.equals(""))
        {
            throw new IllegalArgumentException("first name Error");
        }
        this.firstName=firstName;
    }


}

共 (0) 个答案