Java:在存储到DDC之前验证属性
我正在尝试验证用户的输入。 如果用户输入firstName,程序将进行验证,以确保firstName不为null或空字符串,然后再转到最后一个Java:在存储到DDC之前验证属性,java,Java,我正在尝试验证用户的输入。 如果用户输入firstName,程序将进行验证,以确保firstName不为null或空字符串,然后再转到最后一个entercode hereName。 你能帮我解决这个问题吗? 导入javax.swing.JOptionPane public class implementation{ public static void main(String[] args) { //studentTesting student=new stude
entercode here
Name。
你能帮我解决这个问题吗?
导入javax.swing.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;
}
}
到底是什么问题?这个代码不起作用?它起作用了。我检查了它,但它不工作,因为我认为问题是。程序将弹出一个窗口,让学生输入名字,并在学生输入姓氏之前检查名字以确保其有效。如果firstname中有错误,将打印一个错误以让学生知道。