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.
If I check for
null
with anif
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 thenull
values print to my console.If I check for
null
with anif
statement using the.contains()
method, I get aNullPointerException
If I check for
null
with anif
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 thenull
values print to my console.I cannot use
Array[i].charAt(null)
because thecharAt()
method takes anint
.I cannot use
numS[index].compareTo(null)
because the.compareTo()
method takes aString
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