Issue
I am trying to make a login system in JavaFX and i need to check so that the string for the textArea corresponding to email is the same as the email varibale in my User class. However, the method I use for comparing the string in the textArea with the user details will always return false. I am not sure why. I've tried .equals(), .compareTo, even .charAt() and tested it with 1 letter and it would still return false.
Here is my code for the method. Note that this is the method i use for comparing the string in the textArea to the string in the User.getEmail.
public boolean checkCredentials()
{
return text1.getText().equals(user.getEmail());
}
And here is my code for the method .getEmail() in my User class:
private String email = "1";
public String getEmail()
{
return email;
}
Also, even running this with the input in the textArea as "1", and the string Email in my user class as "1" I still get a return false:
public boolean checkCredentials()
{
return text1.getText().length() == user.getEmail().length();
}
My guess is that somewhere the string from the textArea has something "extra" than the string in the User class. I have tried printing out the length of both strings and i get the same answer.
Minimum code required to re-create, NOTE JavaFX must be installed, and the JavaFX Application template must be used:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application
{
Stage window;
Scene scene1;
String sEmail;
// TextBoxes Dec.
TextArea text1 = new TextArea();
public static void main(String[] args)
{
launch(args);
}
public boolean checkCredentials()
{
return text1.getText().equals(sEmail);
}
@Override
public void start(Stage primaryStage)
{
window = primaryStage;
sEmail = "1";
// TextBoxes Ini.
text1.setMaxWidth(200);
text1.setMaxHeight(25);
text1.setMinHeight(25);
// Button
Button button1 = new Button("Check string");
if(checkCredentials())
{
button1.setOnAction(e -> System.out.println("The email is correct"));
}
else
{
button1.setOnAction(e -> System.out.println("The email is NOT correct"));
}
// Layout1
VBox layout1 = new VBox(20);
layout1.setAlignment(Pos.CENTER);
layout1.getChildren().add(text1, button1);
scene1 = new Scene(layout1, 750, 500);
window.setScene(scene1);
window.setTitle("Hello");
window.show();
}
}
Solution
The current mothod posted above will always return false, unless the string compared to is empty. This is because the check happens when the program is run, meaning that there will always be nothing in the textArea at the start of the program.
To fix this create a EventHandler for the button which will look like this:
button1.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
if(text1.getText().equals(user.getEmail()))
{
window.setScene(scene2);
}
else
{
AlertBox.display("ALERT!", "Please provide correct details");
}
}
});
This method compares the strings when the button is pressed and goes to scene2 when they are identical.
P.S AlertBox is a class i made along with display().
Answered By - Eduard
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)