我正在尝试验证用户的输入。
如果用户输入firstName,程序将在进入最后一个enter code here
名称之前验证以确保firstName不为null或为空字符串。
你能帮我解决一下这个问题吗?
import 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;
}
}