java - 使用 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.
}
解决方案
// 在 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;
}
推荐阅读
- xml - 如何将 xsl 嵌入 xml 以在 Web 浏览器中查看
- c# - 如何在 .net 核心后端使用 PushStreamContent 获取 HttpRequestMessage
- jquery - DataGrid 库:我需要知道回调成功函数,我可以在其中获得 API 调用的响应
- rabbitmq - 同一事件的 RabbitMQ 多个侦听器
- mysql - Liquibase updating mysql db with junk values
- asp.net-mvc - 为什么 ToString 使用不正确的小数分隔符格式化数字
- mysql - 如何在Mysql中选择最大日期小于特定日期的日期的数据
- html - CSS:使用图片上的绝对定位标签使图像适合正确的区域
- asp.net - 在 ASP.NET 零中显示白页的自定义主题
- laravel - 仅请求方法返回原始请求数组