Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从字段中获取文本,如果为空,则执行此操作_Java_If Statement_Equals_Jtextfield_Gettext - Fatal编程技术网

Java 从字段中获取文本,如果为空,则执行此操作

Java 从字段中获取文本,如果为空,则执行此操作,java,if-statement,equals,jtextfield,gettext,Java,If Statement,Equals,Jtextfield,Gettext,我有许多字段需要输入数据。这是一个酒店预订系统,因此如果字段未填充,则必须显示字段为空,并且在未填充字段的情况下无法继续。我想做的是从字段中获取文本,但如果字段为空,则必须将所有字段文本设置为“*请填写所有字段”或显示消息。 我有一些代码不起作用,因为如果字段中没有任何内容,它就无法获取文本。代码如下所示: this.Firstname = NameF.getText(); this.Lastname = NameL.getText(); this.Cou

我有许多字段需要输入数据。这是一个酒店预订系统,因此如果字段未填充,则必须显示字段为空,并且在未填充字段的情况下无法继续。我想做的是从字段中获取文本,但如果字段为空,则必须将所有字段文本设置为“*请填写所有字段”或显示消息。 我有一些代码不起作用,因为如果字段中没有任何内容,它就无法获取文本。代码如下所示:

    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等等于(“”)

您可以创建一个方法来验证varargs中的
字符串
s:

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");
}

您可以创建一个方法来验证varargs中的
字符串
s:

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");
}

你可以这样做

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;
}
public void validateFields(){
for(字符串字段:getNonBlankFields()){
if(字段等于(“”){
showMessageDialog(null,“请填写所有字段”);
返回;
}
}
}
收集非空白字段;
公共集合getNonBlankFields(){
if(this.nonBlankFields!=null){
返回此字段。非空白字段;
}
this.nonBlankFields=newarraylist();
this.nonBlankFields.add(this.lastName);
//添加所有其他字段
this.nonBlankFields.add(this.email);
返回此字段。非空白字段;
}

您可以这样做

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;
}
public void validateFields(){
for(字符串字段:getNonBlankFields()){
if(字段等于(“”){
showMessageDialog(null,“请填写所有字段”);
返回;
}
}
}
收集非空白字段;
公共集合getNonBlankFields(){
if(this.nonBlankFields!=null){
返回此字段。非空白字段;
}
this.nonBlankFields=newarraylist();
this.nonBlankFields.add(this.lastName);
//添加所有其他字段
this.nonBlankFields.add(this.email);
返回此字段。非空白字段;
}

您可以创建一个函数,在循环中为您执行检查

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
}

此方法利用将参数作为数组处理,而无需添加额外代码来显式创建一个数组。

您可以通过创建一个函数来执行循环中的检查

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
}

此方法允许您将参数作为数组处理,而无需添加额外代码来显式创建一个数组。

即使使用map,也可以显示“请填写名字”…当我填写字段时,仍会显示要填写所有字段的消息。我意识到它没有“获取”文本并识别字段中的内容,因为如果发生这种情况,并且在所有字段中都找不到任何内容,这会给我一个巨大的错误尝试:if(this.nonBlankFields!=null){return this.nonBlankFields;}outen即使使用map,也可以显示“请填写名字”…当我填写字段时,它仍然显示要填写所有字段的消息。我意识到它没有“获取”文本并识别字段中的内容,因为如果发生这种情况,并且在所有字段中都找不到任何内容,则会给我一个巨大的错误尝试:if(this.nonBlankFields!=null){返回this.nonBlankFields;}不要考虑使用JooDoad来帮助你验证表单。你可以考虑在<代码>事件监听器< /代码>发生时进行验证,也可以请求YouthPix.GETCuffTeNS()。如果Tebug字段的组件实例化,使用JooDoice来验证TrimeEnter,它可以帮助您验证窗体。当“<代码>事件监听器< /代码>发生时,您可以考虑进行验证,也可以请求YouthPix.GETCuffTeNS()),如果TeXFieldTebug的组件实例化