Tree
Tree

Reputation: 41

My if condition is returning as true, despite double checking my logic

I've been tasked with creating a binary search program for homework (irrelevant to the question, sort of). It was suggested to set the values of the integer array myself, but I decided to take it a little further for my own development. However, it seems there is a logic error, because the condition is passing as true when it seemingly should not be. I am trying to check that a value is not present in an integer array and is not 0, and if that is the case, add it to the array.

I've checked my syntax, changed my logic, and tried to use different loops. Other ways of avoiding 0 and duplicates seem far more complicated, unless there is a fundamental error in my understanding.

Here's what I've got:

int numbers[] = new int[50]; 

        for(int i = 0; i < numbers.length; i++) {
            int addVal = (int)(Math.random()*400+1); //assign addVal to a random int
            boolean duplicate = IntStream.of(numbers).anyMatch(x -> x == addVal); //check if array contains the random value generated and assigned to addVal
            if(addVal != 0 && duplicate == false) { //when the number is not 0 and is not in the list
                numbers[i] = addVal; //add it to the list
            }
        }
Arrays.sort(numbers); //sort the numbers

Here's an example output upon printing the array values:

[0, 0, 11, 21, 34, 40, 51, 53, 54, 61, 76, 91, 114, 120, 166, 173, 199, 209, 249, 266, 277, 295, 301, 312, 340, 349, 355, 365, 366, 392]

I expected it to work as follows: I create an array to store 50 values. For each index, assign addVal a random integer between 0-400 (added 1 to see if it reduces the frequency 0 appears, it seems to have, going larger did not help). Then, check if my numbers array contains the value randomly assigned to addVal. If it does not and the value is also not 0, add it to the array. After all the values have been added, I sort the array (for the binary search).

I would greatly appreciate any help in finding my error.

Also, two side questions: how would I modify this to work with a single enhanced for loop, and why is that sorting the array in the for loop results in significantly more 0s?

Upvotes: 2

Views: 76

Answers (1)

GBlodgett
GBlodgett

Reputation: 12819

The problem is that:

for(int i = 0; i < numbers.length; i++) {
     int addVal = (int)(Math.random()*400+1); 
     boolean duplicate = IntStream.of(numbers).anyMatch(x -> x == addVal); 
     if(addVal != 0 && duplicate == false) { 
         numbers[i] = addVal; //add it to the list
     }
}

If it is a duplicate, you never set anything to the index numbers[i] and it will be a zero by default. You need to decrement i if the given number is a duplicate so that the index will be filled:

for(int i = 0; i < numbers.length; i++) {
     int addVal = (int)(Math.random()*400+1); 
     boolean duplicate = IntStream.of(numbers).anyMatch(x -> x == addVal); 
     if(addVal != 0 && duplicate == false) { 
         numbers[i] = addVal; //add it to the list
     } else {
        i--;
     }
}

Also just a style thing but it is usually preferred to write !duplicate instead of duplicate == false

why is that sorting the array in the for loop results in significantly more 0s?

Because when you sort within the loop, the indexs of the values in the Array change. So say it starts off as:

[2, 0, 0, 0, 0]

Then you sort it and it becomes:

[0, 0, 0, 0, 2]

And the first value will now always be a zero, since you have already populated arr[0]. This keeps on happening, as every time you populate a spot in the Array it will be moved to the end, and the beginning spots will remain zero.

how would I modify this to work with a single enhanced for loop

Simply, you can't.

Upvotes: 3

Related Questions