I am writing an application that checks the login and password, but there was a problem when checking the condition when the user leaves the login or password input fields empty. What's my mistake?

import javafx.application.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.FlowPane; import javafx.stage.*; import javafx.event.*; import javafx.geometry.Pos; public class Main extends Application{ Label lbUsername; Label lbPassword; Label lbInfoPanel; Button btnLogIn; TextField flUsername; PasswordField flPassword; String Username; String Password; public static void main(String[] args) { launch(args); } public void start(Stage myStage){ myStage.setTitle("Login"); FlowPane rootNode = new FlowPane(10,10); rootNode.setAlignment(Pos.CENTER); Scene myScene1 = new Scene(rootNode, 300, 300); myStage.setScene(myScene1); lbUsername = new Label("Enter a Username"); lbPassword = new Label("Enter a password"); btnLogIn = new Button("Log in"); flUsername = new TextField(); flPassword = new PasswordField(); lbInfoPanel = new Label(); Username = new String(); Password = new String(); btnLogIn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { Username = flUsername.getText(); Password = flPassword.getText(); if ((Username == "") || (Username == null) || (Password == "") || (Password == null)) lbInfoPanel.setText("You forget to enter username of password"); } }); rootNode.getChildren().addAll(lbUsername, flUsername, lbPassword, flPassword, btnLogIn,lbInfoPanel); myStage.show(); } } 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Your mistake is that you incorrectly compare strings.

The == operator compares references to objects , not the contents of these objects.

In Java content of strings is compared using the String.equals(...) method.

Suppose you need to compare the two strings String strOne = "SomeTextOne" and String strTwo = "SomeTextTwo" . This is done as follows:

 boolean b = strOne.equals(strTwo); 

As a result, the equals(...) method will return:

  • true if strOne and strTwo lexicographically equal;
  • false if the strOne and strTwo are not lexicographically equal.

There is no need to separately allocate memory for strings ( new String() ), since the getText() method returns a reference to an object of type String (or null (!)).

Also, to check the contents of strings for emptiness , you can use the String.isEmpty() method instead of equals("") .

In the end, you get something like this:

 if (Username == null || Password == null) { lbInfoPanel.setText("Something going wrong"); } else if (Username.isEmpty() || Password.isEmpty()) { lbInfoPanel.setText("You forget to enter username of password"); } 

In general, I have some doubts whether a check for null necessary in this case, since . But it is better to let it be and not be needed, what it will not be and ...


And yet, there is such a thing as a row pool , due to which, in some cases , rows are guaranteed to be represented by the same object. But this is very rare, and you cannot use the == operator to compare the contents of strings.

    There are special methods for validating textual data in Java.

    try replacing your condition with this one:

     if (Username.isEmpty() || Password.isEmpty()){ lbInfoPanel.setText("You forget to enter username of password"); } 

    Method .isEmpty(); will return true you if the string is empty, and the .equals(str2); method is used to .equals(str2); string between each other .equals(str2);

     //вот пример: String name1 = "User"; String name2 = "Login"; String name3 = "User"; String name4 = "login"; //Присвоется значение True так как строки идеентичны boolean check1 = name1.equals(name3); //Присвоется значение False так как строки Login и login не идеентичны, ведь не забываем то что Java чувствителен к регистру. boolean check2 = name2.equals(name4); //присвоется значение False так как строка не пустая и содержит какой-то текст boolean check3 = name3.isEmpty();