How can I check for null at a specific index of a String Array to change its value?

267 Views Asked by At

I am trying to print an array of randomly generated sets of three numbers. Sometimes the sets include a null value when they are printed to the console. Curiously, it is only and always, two of the sixty-six randomly produce sets of three digits that contain a null value.

  1. If I check for null with an if statement using the == operator, then Java interprets it to mean "ignore the value" or "ignore the operation" since the code inside the wiggly brackets is not executed and the null values print to my console.

  2. If I check for null with an if statement using the .contains() method, I get a NullPointerException

  3. If I check for null with an if statement using the .equals() method, then the program may stop upon reaching this check. If I rebuild the program and re-run it, then Java interprets it to mean "ignore the value" or "ignore the operation" since the code inside the wiggly brackets is not executed and the null values print to my console.

  4. I cannot use Array[i].charAt(null) because the charAt() method takes an int.

  5. I cannot use numS[index].compareTo(null) because the .compareTo() method takes a String for an argument.

In an attempt to figure out the reason behind this, I used the Math.Random method or a Random rng = new Random(); Object. This did not change the outcome.

Any assistance will be greatly appreciated.

I checked the post [Check if array is NULL at a Specific Index returns NullPointerException], but it did resolve my issue.

I also modified my original code as below per the aforementioned post, to:

 while (randomNum.size() < 66) {
        for (int index = 0; index <= 2; index++) {
            int n = obj.rng.nextInt(4)+1;
            String s = Integer.toString(n);

            if (s == null)
                {n = obj.rng.nextInt(4)+1;
                s = Integer.toString(n);
                numS[index] = s;}

and also to:

int n = obj.rng.nextInt(4)+1;
            String s = Integer.toString(n);

            if (s.equals(null))
                {n = obj.rng.nextInt(4)+1;
                s = Integer.toString(n);
                numS[index] = s;}

But that did not resolve my issue either.

Here is my code:

import java.util.*;
import java.lang.*;
public class TestingTests{ 
public static void main(String[] args) { //main() method.
    obj.uniqueString();
} //end of main() method

void uniqueString() {
    while (randomNum.size() < 66) {
        for (int index = 0; index <= 2; index++) {
            int n = obj.rng.nextInt(4)+1;
            numS[index] = Integer.toString(n);

            if (numS[index] == null)
                {n = obj.rng.nextInt(4)+1;
                numS[index] = Integer.toString(n);}
            else if (randomNum.size() == 66)
                {ArrayList randomList = new ArrayList(randomNum);
                System.out.println("This is the size of randomList: " + randomList.size());
                Collections.sort(randomList);
                System.out.println("This is a randomList: " + randomList);
                break;
            }
            else randomNum.add(Arrays.toString(numS));
        }
    }
}

int temp = 0, count = 0;
static TestingTests obj = new TestingTests();
String[] numS = new String[3];
Random rng = new Random();
Set<String> randomNum = new HashSet<>();
ArrayList randomList = new ArrayList(randomNum);

}// end of MyClass

Here is an example of the output with the null values that I am trying to rid:

This is the size of randomList: 66
This is a randomList: [[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 1, 4], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 2, 4], [1, 3, 1], [1, 3, 2], [1, 3, 3], [1, 3, 4], [1, 4, 1], [1, 4, 2], [1, 4, 3], [1, 4, 4], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 1, 4], [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 2, 4], [2, 3, 1], [2, 3, 2], [2, 3, 3], [2, 3, 4], [2, 4, 1], [2, 4, 2], [2, 4, 3], [2, 4, 4], [3, 1, 1], [3, 1, 2], [3, 1, 3], [3, 1, 4], [3, 1, null], [3, 2, 1], [3, 2, 2], [3, 2, 3], [3, 2, 4], [3, 3, 1], [3, 3, 2], [3, 3, 3], [3, 3, 4], [3, 4, 1], [3, 4, 2], [3, 4, 3], [3, 4, 4], [3, null, null], [4, 1, 1], [4, 1, 2], [4, 1, 3], [4, 1, 4], [4, 2, 1], [4, 2, 2], [4, 2, 3], [4, 2, 4], [4, 3, 1], [4, 3, 2], [4, 3, 3], [4, 3, 4], [4, 4, 1], [4, 4, 2], [4, 4, 3], [4, 4, 4]]

Process finished with exit code 0
3

There are 3 best solutions below

1
Daniel Hernández On
void uniqueString() {
    while (randomNum.size() < 64) {
        String[] numS = new String[3];
        for (int index = 0; index < numS.length; index++) {
            int n = obj.rng.nextInt(4) + 1;
            numS[index] = Integer.toString(n);
        }
        randomNum.add(Arrays.toString(numS));
    }
    ArrayList<String> randomList = new ArrayList<>(randomNum);
    System.out.println("This is the size of randomList: " + randomList.size());
    Collections.sort(randomList);
    System.out.println("This is a randomList: " + randomList);
}

Change it to a max of 64 (V3′​(4)=4^3), in your code it could return 66 because of the null values.

7
Vaikan Peddi On

As you know, null is a keyword specific to reference types and not for primitive types. So a small Java function to check if a string array is null at a particular index, could go something like this:

public class Main {
  public static void main(String[] args) {
    String[] arr = {"Stack", null, "Overflow"};
    System.out.println(isNull(arr, 1));
  }
  
  public static boolean isNull(String[] arr, int index) {
    arr[index] == null ? return true : return false;
    }
  }
}

And as you can see in this simplified (maybe super-simplified!) example, the output will be

true

So hope this answer helps! Thanks! Please correct me if I didn't exactly get you.

7
Reilas On

"I am trying to print an Array of randomly generated sets of three numbers, and sometimes the sets include a null value ..."

There are a few inconsistencies with your code.

Consider the following.

iteration 1, index= 0, n= 2, numS= [2, null, null], randomNum= [[2, null, null]]
iteration 1, index= 1, n= 4, numS= [2, 4, null], randomNum= [[2, 4, null], [2, null, null]]
iteration 1, index= 2, n= 1, numS= [2, 4, 1], randomNum= [[2, 4, null], [2, 4, 1], [2, null, null]]
iteration 2, index= 0, n= 3, numS= [3, 4, 1], randomNum= [[2, 4, null], [2, 4, 1], [3, 4, 1], [2, null, null]]
iteration 2, index= 1, n= 3, numS= [3, 3, 1], randomNum= [[2, 4, null], [2, 4, 1], [3, 4, 1], [3, 3, 1], [2, null, null]]
iteration 2, index= 2, n= 1, numS= [3, 3, 1], randomNum= [[2, 4, null], [2, 4, 1], [3, 4, 1], [3, 3, 1], [2, null, null]]
iteration 3, index= 0, n= 3, numS= [3, 3, 1], randomNum= [[2, 4, null], [2, 4, 1], [3, 4, 1], [3, 3, 1], [2, null, null]]
iteration 3, index= 1, n= 4, numS= [3, 4, 1], randomNum= [[2, 4, null], [2, 4, 1], [3, 4, 1], [3, 3, 1], [2, null, null]]
iteration 3, index= 2, n= 2, numS= [3, 4, 2], randomNum= [[2, 4, null], [2, 4, 1], [3, 4, 1], [3, 3, 1], [3, 4, 2], [2, null, null]]
...

"I am trying to print an array of randomly generated sets of three numbers. ..."

You can use the following example.

List<Set<Integer>> list = new ArrayList<>();
Set<Integer> set;
Random random = new Random();
for (int count = 1; count <= 66; count++) {
    set = new HashSet<>();
    do {
        set.add(random.nextInt(1, 5));
    } while (set.size() != 3);
    list.add(set);
}
System.out.println(list);

Output

[[1, 2, 4], [1, 3, 4], [2, 3, 4], [1, 3, 4], [1, 3, 4], [1, 2, 3], [1, 3, 4], [1, 3, 4], [1, 3, 4], [1, 2, 4], [1, 2, 4], [2, 3, 4], [2, 3, 4], [2, 3, 4], [1, 2, 4], [2, 3, 4], [1, 2, 3], [2, 3, 4], [1, 2, 3], [1, 2, 4], [1, 2, 3], [2, 3, 4], [1, 2, 4], [1, 2, 4], [1, 2, 4], [1, 2, 3], [1, 3, 4], [1, 2, 4], [2, 3, 4], [1, 3, 4], [1, 3, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3], [1, 2, 3], [2, 3, 4], [1, 2, 4], [1, 2, 3], [2, 3, 4], [1, 3, 4], [2, 3, 4], [2, 3, 4], [1, 2, 4], [2, 3, 4], [2, 3, 4], [1, 2, 4], [1, 2, 4], [1, 2, 4], [2, 3, 4], [1, 2, 4], [1, 2, 4], [2, 3, 4], [1, 2, 4], [1, 2, 4], [1, 3, 4], [1, 2, 3], [2, 3, 4], [2, 3, 4], [2, 3, 4], [1, 2, 3], [1, 3, 4], [1, 2, 4], [2, 3, 4], [2, 3, 4], [2, 3, 4], [2, 3, 4]]