0

I created a program where the usernames and paswords are saved in arrays (user and pas). Now i want the user to login, so i want to use linear search to see if the account's details are correct or not. I'm not sure what i'm doing wrong here:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.PasswordField ;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Main extends Application {

    Button btnLogin,btnAanmaken;
    TextField txtUsername;
    PasswordField Password;
    Label lblUsername,lblPassword;
    int index;
    String[] user = new String[10];
    String[] pas = new String[10];
    int searchInt; // search key
    int position; // location of search key in array

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Aanmelden");
        btnLogin = new Button();
        btnLogin.setText("Login");

        btnAanmaken = new Button();
        btnAanmaken.setText("Aanmaken");

        txtUsername=new TextField();
        txtUsername.setMaxWidth(200);

        Password=new PasswordField();
        Password.setMaxWidth(200);

        lblUsername=new Label();
        lblUsername.setText("Gebruikersnaam");

        lblPassword=new Label();
        lblPassword.setText("Paswoord");



            btnAanmaken.setOnAction(e ->{
       if (index <= 10){
            index++;
            user[index]=txtUsername.getText();
            pas[index]=Password.getText();

           AlertBox.display("Welkom","Account werd succesvol aangemaakt: \n Gebruikersnaam: " + user[index] + " \n Paswoord: " + pas[index]  );
           //AlertBox.display("Welkom", "U bent nu ingelogd");
       }else{
          System.out.println("U heeft het maximaal aantal accounts bereikt");
       } 
       });

       //btnLogin.setOnAction(e -> {
       //        if (txtUsername.getText().equals("user") && Password.getText().equals("pass")){
       //            AlertBox.display("Welkom", "U bent nu ingelogd");
       //         }
       //            else{
       //            AlertBox.display("Fout ", "De opgegeven gebruikersnaam of het opgegeven wachtwoord is onjuist."); 
      //              }
      //      });


        btnLogin.setOnAction(e -> linearSearch( String searchKey ) )
        VBox layout = new VBox();
        layout.getChildren().addAll(lblUsername,txtUsername,lblPassword,Password,btnAanmaken,btnLogin);
        Scene scene = new Scene(layout, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    // perform a linear search on the data
   public int linearSearch( String searchKey )
   {
      // loop through array sequentially
      for ( int index = 0; index < user.length; index++ )
         if ( user[ index ] == searchKey )
            return index; // return index of integer

      return -1; // integer was not found      
   } // end method linearSearch
}
petie
  • 77
  • 3
  • 13

1 Answers1

0

Don't Use == to compare strings!. Instead use String.equals(String s)

Here is how your would implement it in your code:

just change:

 if ( user[ index ] == searchKey )

to:

if ( user[ index ].equals(searchKey) )

Hope this helped.

Paul
  • 670
  • 7
  • 19