1

I want to check whether an Array element is null.

I have initialized an array of String which has a size of 2. I looped through the array and check whether an array element is null. If it's null, I will add a String "a" to that position only.

My codes below will produce the following output:

1=a
2=a

Code:

public class CheckArrayElementIsNull {
    public static void main(String[] args) {
        String[] arr = new String[2];       
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] == null) {
                arr[i] = "a";
            }
            System.out.println(i + "=" + arr[i]);   
            if(arr[i] == null) {
                System.out.println(i + "= null");
            }
        }
    }
}

I tried to add a break after my if condition but is not printing out anything.

 public class CheckArrayElementIsNull {
    public static void main(String[] args) {
        String[] arr = new String[2];       
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] == null) {
                arr[i] = "a"; 
                break;
            }
            System.out.println(i + "=" + arr[i]);   
            if(arr[i] == null) {
                System.out.println(i + "= null");
            }
        }
    }
}

I expected this output:

1=a
2=null
3
  • You should check and print the null value before you assign to it, otherwise that element won't be null when you check it the second time. Commented Aug 26, 2014 at 8:50
  • 1
    Why are you starting from 1 rather than 0? Commented Aug 26, 2014 at 8:51
  • ok maybe I should start it with 0. sorry.edited Commented Aug 26, 2014 at 8:52

3 Answers 3

2

You have a few problems in your loop.

    for(int i = 1; i < arr.length - 1; i++) { //<---- This does not iterate over the entire array, leaving out the first and last elements
        if(arr[i] == null) {
            arr[i] = "a"; 
            break; //<---- This terminates the loop entirely, if you want to stop all instructions past this one try using continue instead
        }
        System.out.println(i + "=" + arr[i]);   
        if(arr[i] == null) { //This code is unreachable as arr[i] is initialized if it was detected as null before
            System.out.println(i + "= null");
        }else{
            System.out.println(i + "=" + arr[i]); 
        }
    }

Instead you should try

 for(int i = 0; i < arr.length; i++) {
    if(arr[i] == null) {
        arr[i] = "a";
        System.out.println(i + "= null");
        break;
    }
    System.out.println(i + "=" + arr[i]); 
}
Sign up to request clarification or add additional context in comments.

6 Comments

That will still assign all values in the array to a.
As far as I understand the question that is the desired effect: setting all null-values to "a" instead
@Stefan not all values, only those with null
setting the first null value found to a in the array.
@tz reur Oh, only the first? Fixed that :)
|
0

It is because break will break the iteration of the for loop, and exit out of loop immediately. You should change your design a bit. From what I understand you are trying to achieve (Assigning "a" to the first null element encountered and stop, print all elements in the array) following should work:-

public class CheckArrayElementIsNull {

  public static void main(String[] args) {
     String[] arr = new String[3];       
     boolean flag=true; //use a flag that will check if null is encountered
     for(int i = 0; i < arr.length; i++) {
       if(arr[i] == null && flag) { // first time flag will be true
         arr[i] = "a"; 
         flag=false; //set flag to false , to avoid further assignment of 'a' to null values
        }
        System.out.println(i + "="+ arr[i]);    //print array
        }
     }
  }

2 Comments

Wouldn'T this cause a NullPointerException in System.out.println if the array contains more than one null element?
@Dragondraikk No it wont , it will simply print null, this is because i am passing an object of String type (which has value null) , so the compiler knows which toString() method to call. NPE would take place when I would write System.out.println(null), because in this case compiler cannot deduce which toString() method to call.
0

Try assigning the value at the index you expect not to be null before you enter your for loop:

String[] arr = new String[3];
arr[1] = "a";
for (int i = 0; i < arr.length; i++)
{
  if (arr[i] == null)
  {
    System.out.println(i + "=null");
  }
  else
  {
    System.out.println(i + "=" + arr[i]);
  }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.