2016-03-21 93 views
0

我現在正在努力檢查我的所有InputFields是否爲== ""JavaFX Sqlite檢查TextField是否爲空

我在這裏找到了很多解決方案,但不知何故,它不工作。

我有一個RegistrationModal和一個RegistrationController。

在我tryed檢查這樣的領域的模式:

public boolean RegisterUser(String user, String vorname, String nachname) throws SQLException { 

    PreparedStatement preparedStatement = null; 

    if (user == "" || vorname == "" || nachname == "") { 
     System.out.println("Fill in all fields"); 
     return false; 
    } 
// Here follows the SQL query the prepared statement and exequte 
} 

但如果聲明它它絕不會使用這個,我不知道爲什麼?

我也tryed趕在控制器中的空元素:

public void Absenden(ActionEvent event) { 
    try { 
     if (registerModel.RegisterUser(txt_user.getText(), txt_vorname.getText(), 
       txt_nachname.getText())) { 


      if (registerModel.RegisterUser(txt_user.getText().trim().isEmpty(), txt_vorname.getText().trim().isEmpty(), 
        txt_nachname.getText().trim().isEmpty())) { 
       System.out.println("False! Do something?"); 
      } 


      // Here opens the new window with a notification Registration was successfull 
      ((Node) event.getSource()).getScene().getWindow().hide(); 
} 

在這裏,我有錯誤,所以這就是爲什麼是兩個,如果這似乎已經怪異的說法,我不得不自動生成一個新的方法,模型,使這個與布爾人一起工作,但它從未使用過這個if語句。

我試着只給出相關的代碼,否則會更多。

也許有人有一個想法。

感謝

回答

0

如果有一個或多個空格作爲輸入你的狀態將會失敗。

你應該總是trim()然後檢查長度爲0

user.trim().length==0 

此外,如果你想查詢的內容總是使用equals,而不是==

+0

tr用等號代替==來做,我可以繼續工作,非常感謝。將在5分鐘內接受。 – Roman

0

你應該嘗試像此

   if(use.trim.length<0) 

       { 
        // todo code !! 
       }