2013-07-12 44 views
0

我有許多需要輸入數據的字段。這是一個酒店預訂系統,所以如果這些字段沒有填滿,它必須顯示它們是空的,並且不填充它們就不能繼續。我想要做的是從字段中獲取文本,但如果它們爲空,則必須將所有字段文本設置爲「*請填寫所有字段」或顯示消息。 我有一些代碼不工作,因爲如果字段中沒有任何內容,它就不能獲取文本。代碼如下所示:從字段中獲取文本,但如果空白,請執行此操作

this.Firstname = NameF.getText(); 
     this.Lastname = NameL.getText(); 
     this.Country = Countr.getText(); 
     this.IDtype = IDTy.getText(); 
     this.PassportNo = PassNo.getText(); 
     this.IDNo = IDNumber.getText(); 
     this.Addr1 = Add1.getText(); 
     this.Addr2 = Add2.getText(); 
     this.AreaCode = Integer.parseInt(Area.getText()); 
     this.TelNo = Tel.getText(); 
     this.CellNo = Cell.getText(); 
     this.Email = Em.getText(); 
    } 
    if (this.Firstname.equals("") || this.Lastname.equals("") || this.Country.equals("") || this.IDtype.equals("") || this.IDNo.equals("") || this.Addr1.equals("") || this.Addr2.equals("") || this.AreaCode == 0 || this.TelNo.equals("") || this.CellNo.equals("") || this.Email.equals("")) { 
     JOptionPane.showMessageDialog(null, "Please fill in all fields"); 
    } 

不知道如果我要問這一個問題,但如果有沒有這麼多||運營商做出了一個更簡單的方法?就像if this.Firstname,this.Lastname,etc.equals("")

+0

考慮使用JGoodies來幫助您驗證表單。 – vels4j

+0

您可能會考慮在發生'EventListener'時進行驗證,也可以要求提供yourContainer.getComponents(),並且如果組件instanceof textfield驗證那裏 – nachokk

回答

1

你可以通過創建一個函數在循環中爲你做檢查來做到這一點;

public boolean isAnyEmpty(String... strArr){ 
    for(String s : strArr){ 
     if(s.equals("")) return true; 
    } 
    return false; 
} 

然後用

if(isAnyEmpty(this.Firstname, this.lastName, this.Country, /* rest of your strings */)){ 
    //your code 
} 

該方法利用的varargs讓你把這些參數作爲一個數組稱呼它,而無需在額外的代碼添加到明確創建一個。

0

您可以創建將在可變參數的味道驗證String個方法:

public boolean validateString(String ... stringsToValidate) { 
    for (String validString : stringsToValidate) { 
     if (...) { //logic to validate your String: not empty, not null, etc 
      return false; 
     } 
    } 
    return true; 
} 

然後就這樣稱呼它:

//add all the strings that must be validated with rules defined in validateString 
if (!validateString(NameF.getText(), NameL.getText(), Countr.getText(), ...) { 
    JOptionPane.showMessageDialog(null, "Please fill in all fields"); 
} 
3

你可以做這樣的事情。

public void validateFields() { 
    for (String field : getNonBlankFields()) { 
     if (field.equals("")) { 
      JOptionPane.showMessageDialog(null, "Please fill in all fields"); 
      return; 
     } 
    } 
} 

Collection<String> nonBlankFields; 
public Collection<String> getNonBlankFields() { 
    if (this.nonBlankFields != null) { 
     return this.nonBlankFields; 
    } 
    this.nonBlankFields = new ArrayList<String>(); 
    this.nonBlankFields.add(this.lastName); 
    // add all of the other fields 
    this.nonBlankFields.add(this.email); 
    return this.nonBlankFields; 
} 
+0

即使您使用map <「FirstName」,this.firstName>,也可以顯示「Please填寫名字「... – vels4j

+0

當我填寫字段時,它仍然顯示填寫所有字段的消息。我意識到,它沒有'得到'的文本,並確認在領域是什麼,因爲如果發生這種情況,並沒有發現所有的領域,它給了我一個巨大的錯誤 – Gordy

+0

嘗試採取: if(this.nonBlankFields!= null){ return this.nonBlankFields; } out – richjhall18

相關問題