JavaFX中的线性搜索

时间:2015-11-15 13:00:57

标签: java javafx

我创建了一个程序,其中用户名和密码保存在数组中(user和pas)。现在我希望用户登录,所以我想使用线性搜索来查看帐户的详细信息是否正确。我不确定我在这里做错了什么:

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
}

1 个答案:

答案 0 :(得分:0)

请勿使用==来比较字符串!而是使用String.equals(String s)

以下是您在代码中实现它的方式:

只是改变:

 if ( user[ index ] == searchKey )

为:

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

希望这会有所帮助。