1

I need to check Whether My ArrayList is null or Empty or Conatins any special character? ArrayList Debugging Image.

enter image description here

if(!lotNumArrList.contains(null)&&lotNumArrList!=null&&!lotNumArrList.isEmpty()){
        {
        }
}
2
  • 1
    My Array List Conatins Special character(,).I Commented Feb 4, 2016 at 9:25
  • You ArrayList is not empty or null. It has 3 elements. Commented Feb 4, 2016 at 9:33

6 Answers 6

1

Try this:

 boolean hasSpecialChar = false;
            if (lotNumArrList != null && lotNumArrList.size() > 0) {
                for (int i = 0; i < lotNumArrList.size(); i++) {
                    String value = lotNumArrList.get(i).trim();
                    if (value.length() > 0 && value.contains(",")) {
                        hasSpecialChar = true;
                        break;
                    }
                }
            }
Sign up to request clarification or add additional context in comments.

Comments

0

try the below code:

  Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
    boolean hasSpecialChar = false;
                if (lotNumArrList != null && lotNumArrList.size() > 0) {
                    for (int i = 0; i < lotNumArrList.size(); i++) {
                        String str = lotNumArrList.get(i).trim();
                        Matcher m = p.matcher(str);
                        boolean b = m.find();
                        if (b) {
                            hasSpecialChar = true;
                            break;
                        }
                    } 
}

Comments

0
Pattern p = Pattern.compile("[^a-zA-Z]");
if(lotNumArrList!=null && !lotNumArrList.isEmpty() && !p.matcher(string).find()){

}

Comments

0
  if (lotNumArrList.size()>0){
            boolean isNull = false;
            for (int i = 0;i>lotNumArrList.size();i++){
                String itemValue = lotNumArrList.get(i).trim();
                if (itemValue==null||itemValue.equalsIgnoreCase("")||itemValue.length()==0){
                    isNull = true;
                    break;
                }else {
                    isNull = false;
                }

            }
            if (!isNull) {
                mDBHelper.getWritableDatabase();
                mDBHelper.updateFIQTY(fiGoods, fiQty,fiUnWgt,totWgt); mDBHelper.closeDatabase(); 
            }
        }

7 Comments

see my debuging image.. Its showing Size as 3. But it doesnt have any values
Please tell me, Object of ArrayList you are using?
if(!lotNumArrList.contains(null)&&lotNumArrList!=null&&!lotNumArrList.isEmpty()){ if (fiQty.isEmpty() || fiUnWgt.isEmpty()) { nullConfirmation(); } else { mDBHelper.getWritableDatabase(); mDBHelper.updateFIQTY(fiGoods, fiQty,fiUnWgt,totWgt); mDBHelper.closeDatabase(); }
It has size as 3.. But it having only empty space inside it.
Please show your code, where you have defined your array list.
|
0

Assuming your ArrayList is ArrayList, try the below code

    boolean isArrValid = true;
    String pattern = "^[a-zA-Z0-9]*$";
    if (lotNumArrList != null && lotNumArrList.size() > 0) {
        for (String str : lotNumArrList) {
            if (str == null || !str.matches(pattern)) {
                isArrValid = false;
                break;
            }
        }
    } else {
        isArrValid = false;
    }

    if(isArrValid) {
      //Your action
    }

Hope it helps!

Comments

0

this is not tested yet. try if it works.

if (lotNumArrList == null || lotNumArrList.size == 0){
    // your code here if null or empty
} else {
    String pattern = "[^a-zA-Z0-9]"; // you can change this one on what you want to have in your string

    for (String s : list)
            if(s.matches(pattern)){
            // your code here if found special characters
            }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.