我的 IF ELSE 语句为什么默认反向?

huangapple go评论67阅读模式
英文:

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

你可以使用 http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isEmpty-java.lang.CharSequence-

或者编写自己的检查代码,验证字符串不为null且含有字符。

你也可以查看 StringUtils#isBlank。

英文:

You can use http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isEmpty-java.lang.CharSequence-

Or write your own check that string is not null and has some characters.

You can also checkout StringUtils#isBlank

huangapple
  • 本文由 发表于 2020年4月10日 11:16:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61133512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定