英文:
Why is my IF ELSE statement defaulting backwards?
问题
我对为什么会发生这种情况感到非常困惑。当我运行这段代码并将txtFirstName字段留空时,它返回true,尽管显然不应该这样。
@FXML
private void signUp(ActionEvent event) {
if (txtFirstName.getText() != null) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("IT WORKS");
alert.setHeaderText("IT WORKS!");
alert.setContentText("IT WORKS!!!!!");
alert.showAndWait();
} else {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Warning");
alert.setHeaderText("There was a problem creating your account");
alert.setContentText("Please fill out every field with the minimum requirements!");
alert.showAndWait();
}
}
当我在“名字”字段中输入一个值时,我得到了“否则”语句,但我应该得到“如果”语句... 我是疯了还是这反过来了?
英文:
I am very confused as to why this is happening. When I run this code and leave the txtFirstName field blank, it returns true even though it clearly shouldn't.
@FXML
private void signUp(ActionEvent event) {
if (txtFirstName.getText() != null) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("IT WORKS");
alert.setHeaderText("IT WORKS!");
alert.setContentText("IT WORKS!!!!!");
alert.showAndWait();
} else {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Warning");
alert.setHeaderText("There was a problem creating your account");
alert.setContentText("Please fill out every field with the minimum requirements!");
alert.showAndWait();
}
}
When I do type a value into the First Name field, I get the else statement when I should be getting the if statement... Am I insane or is this backwards?
答案1
得分: 1
假设您的“First Name”字段是一个字符串,您可以使用:
txtFirstName.getText().isEmpty();
希望这解决了您的问题!
英文:
Assuming your First Name field is a String, you can use
txtFirstName.getText().isEmpty();
Hope this solves your question!
答案2
得分: 1
或者编写自己的检查代码,验证字符串不为null且含有字符。
你也可以查看 StringUtils#isBlank。
英文:
Or write your own check that string is not null and has some characters.
You can also checkout StringUtils#isBlank
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论