首页 > 解决方案 > 使用 OR 检查 if 语句中的多个空值并将错误存储在数组中

问题描述

当用户提交表单时,我想检查表单中的多个文本字段并检查它是否为空,并且它必须显示哪个输入字段为空白

String A = request.getparameter("a");
String B = request.getparameter("b");
String C = request.getparameter("c");
if(A==null || B==null || C==null)
{

//Here I want to store the corresponding errors in an arrayfield and it should display which field is empty.

}

标签: java

解决方案


// 在 onCreate 方法中执行此操作

   //make this variable globle

   ArrayList<MyVariableNameValue> nullStringList=new ArrayList<>();
    for (MyVariableNameValue nullString: checkNull()) {
        Log.d("nullsize", "onCreate: "+nullString.getName());
    }
    //up to here

//创建getter方法类,将变量名和值存储在mainactivity中或mainactivity之外

 public class MyVariableNameValue{
    String name,value;
    public MyVariableNameValue(String name,String value){
        this.name=name;
        this.value=value;
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }
}

//在maiactivity中创建一个返回空变量值和名称的方法

 public ArrayList<MyVariableNameValue> checkNull(){
    nullStringList.clear();
  String A = request.getparameter("a");
  String B = request.getparameter("b");
  String C = request.getparameter("c");
    ArrayList<MyVariableNameValue> checkString = new ArrayList<>();
    checkString.add(new MyVariableNameValue("A",A));
    checkString.add(new MyVariableNameValue("B",B));
    checkString.add(new MyVariableNameValue("C",C));
    for (MyVariableNameValue myString: checkString) {
        if(TextUtils.isEmpty(myString.getValue())){
            nullStringList.add((myString));
        }

    }
    return nullStringList;
}

推荐阅读