gpt4 book ai didi

java - 创建帐户时的验证 JAVA

转载 作者:行者123 更新时间:2023-12-01 11:29:42 25 4
gpt4 key购买 nike

因此,我的程序中有一个特定部分,我可以在其中创建一个帐户,并且创建的帐户将被插入到我的数据库中。我正在尝试编写一些*引用代码的代码:

public void actionPerformed(ActionEvent e) {
String user = userField.getText().trim();
String pass = passField.getText().trim();
String conPass = confirmPass.getText().trim();
try{

// TODO Auto-generated method stub
if(e.getSource()==submit){

if (user.equals(user)&&pass.length()==0){
JOptionPane.showMessageDialog(null, "Fill in the empty field!");
}//check if the pass field is blank
else if(user.length()<5){
JOptionPane.showMessageDialog(null,"Username must be at least 5 characters!");
}
else if(user.equals(user)&&pass.equals(conPass)&&pass.length()!=0){
String sqlLogin = "insert into tblLogin (username,pssword) values ('"+user+"','"+pass+"')";;
getQuery(sqlLogin);
JOptionPane.showMessageDialog(null, "Account Successfully Created!");
create.dispose();
GUI gui = new GUI();
}//if(pass.equals(conPass))
else if(user.length()==0&&pass.length()==0){
JOptionPane.showMessageDialog(null, "Fill in the empty field!");
}//check if both fields are blank
else if (user.length()==0 &&pass.equals(pass)){
JOptionPane.showMessageDialog(null, "Fill in the empty field!");
}//check if user field is blank
else if(user.equals(user)&&pass!=conPass){
JOptionPane.showMessageDialog(null, "Password do not match!");
}//check if password and confirm pass matches
}

我真的不知道如何说这个问题,但是看看 ifelse if 语句,如果用户满足这些条件之一,程序应该打印JOptionPane 的东西。除了第二个 else if 。

您可能想知道为什么我将这些代码放在 else if

else if(user.equals(user)&&pass.equals(conPass)&&pass.length()!=0){
String sqlLogin = "insert into tblLogin (username,pssword) values ('"+user+"','"+pass+"')";;
getQuery(sqlLogin);
JOptionPane.showMessageDialog(null, "Account Successfully Created!");
create.dispose();

原因是,当我尝试将其放入 if 语句时,我的程序出现了一些逻辑错误。请帮助我编写代码,谢谢:)请随意为我编写新代码:DD

最佳答案

我可能会尝试这样的事情:

public static boolean isSet(String s){
if(s==null || "".equals(s)) return false;
return true;
}
//.... your validation here
if(isSet(user) && isSet(pass) && isSet(conPass) && pass.equals(conPass)){
//create account
}else{
//smth wrong eg. if(!pass.equals(conPass) { //wrongpass }
}

关于java - 创建帐户时的验证 JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30514750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com