代码实现Java Swing下用户输入文本框后格式查错
满足要求:
1.密码,姓名,地址必须输入,地址和电话至少输入一个。
2.密码在8-16位之间,姓名为字符串,电话11位,地址包含@和. 。
格式正确后跳转页面并将所有信息写入txt文件
public void createUser(){
String contents;
String userName;
try {
FileWriter fileWriter = new FileWriter("user.txt",true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
//judge if the input is valid
if("".equals(textWord.getText())==false && "".equals(FirstName.getText())==false && "".equals(SurName.getText())==false) { //password,FirstName,SurName are all input
if("".equals(Address.getText())==false || "".equals(TelNum.getText())==false) {//address or telephone are input
if(7<textWord.getText().length() && textWord.getText().length()<17) {//passwords are between 8 and 16
if(Address.getText().contains("@") && Address.getText().contains(".")) {
if(TelNum.getText().length() ==11 && TelNum.getText().matches("\\d{11}")) {
if(FirstName.getText().matches("[a-zA-Z]+")==true && SurName.getText().matches("[a-zA-Z]+")==true) {
//change the page
controlVariable = 1;
JOptionPane.showMessageDialog(null, "You have been our membership!");
//generate and print the membership number
StringBuilder str=new StringBuilder();
for(int i=0;i<8;i++){
str.append(new Random().nextInt(10));
}
String num=str.toString();
System.out.println("Your membership number is: "+num);
System.out.println(textWord.getText());
//write in user.txt
contents = num+" " +textWord.getText()+" " +"0 "+FirstName.getText()+" " +SurName.getText()+" " +Address.getText()+" " +TelNum.getText();
bufferedWriter.write(contents);
bufferedWriter.write("\r\n");
bufferedWriter.close();
fileWriter.close();
dispose();
}
else {//firstname or surname are invalid
JOptionPane.showMessageDialog(null, "Your firstname or surname are invalid");
}
}
else {//telephone number are invalid
JOptionPane.showMessageDialog(null, "Your telephone number are invalid");
}
}
else {//address are invalid
JOptionPane.showMessageDialog(null, "Your address are invalid");
}
}
else {//password are not between 8 and 16
JOptionPane.showMessageDialog(null, "Your passwords must be between 8 and 16");
}
}
else {//no address or telephone
JOptionPane.showMessageDialog(null, "Address or Telephone must be filled out ");
}
}
else {//no password/FirstName/SurName
JOptionPane.showMessageDialog(null, "Your password/FirstName/SurName must be filled out ");
}
}catch (IOException e) {
System.out.println("Errors occured");
System.exit(1);
}
}