I know how to do this for numeric datatypes but please tell my for string arrays.
Say I have a string array of 10 elements in java. Some elements contain words but others contain NOTHING.
How do I run a for loop which prints only the non empty elements?
>Solution :
To print empty elements:
for (int i = 0; i < arr.length; i++) {
if (arr[i] == null || arr[i].trim().isEmpty()) {
System.out.println("Element " + i " + " is null or empty.");
}
}
The print non-empty elements:
for (int i = 0; i < arr.length; i++) {
if ( arr[i] != null && !arr[i].trim().isEmpty())) {
System.out.println(arr[i]);
}
}