Lewis Menelaws
Lewis Menelaws

Reputation: 1186

Find missing value in array using a for loop

So I am trying to make a for loop that searches through an array and returns how many missing values it has found.

Here is my method:

    public void questionsMissed() {
            int missedQuestions = 0;
            for (int i = 0; i < 20; i++) {
                if (answers[i] == null){
                    missedQuestions++;
                }
            }
 System.out.println("You have missed " + missedQuestions + " questions.");
        }

The error is where the if statement is. I am assuming it is because of the null. How can I go around finding missing values?

ERROR: incomparable types: int and

Upvotes: 2

Views: 1267

Answers (3)

Ren&#233; Link
Ren&#233; Link

Reputation: 51353

You compare a primitive type int to null. Primitive types are not objects and always have a value. Thus you can not compare them to null.

If you use an Integer[] you can mark missed questions by inserting null or you can compare them with '\0'.

But since you already use character literals I would use Character instead.

When you use a primitive wrapper type your methods will also get much easier. E.g.

Character[] answers = new Character[20];

....

public void questionsMissed() {
    int missedQuestions = Collections.frequency(Arrays.asList(answers), '\0');
    System.out.println("You have missed " + missedQuestions + " questions.");
}

Upvotes: 1

Am_I_Helpful
Am_I_Helpful

Reputation: 19158

Two things :-

There is nothing like missing values which you can compare using null. You can compare it to 0 as those defined array members are initialised to 0 (THANKS to dasblinkenlight for correcting me,I was confused with C),but that appears to be a poor decision/choice.

I'd recommend putting -1 for the missing values as is generally done in sentinel condition checking and then compare values like :-

public void questionsMissed() {
    int missedQuestions = 0;
    for (int i = 0; i < 20; i++) {
        if (answers[i] == -1){
          System.out.println("OOPS,the element here is missing!");
        }
    }
}

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726559

You get the error because you are trying to compare a primitive data type, int, to null. Only reference types can be compared to null. Primitive types always have a value - they cannot be null, so the compiler tells you that the comparison does not make sense.

There are two ways you can go about fixing this:

  • Switch the type of answers to Integer - This would let you check its values for null, at the expense of wrapping primitive integers into by-reference wrappers.
  • You can designate an integer value, say, -1, as "missing" - This would let you keep int as the type of answer[]'s element, but there would be a special number that must be used everywhere in your program.

Since you are using char values for your answers, I would use the second approach, but first I would make answers and correctAnswers arrays of char. There is a convenient "special value" for the unused value - the null character \0':

if (answers[i] == '\0') {
    ...
}

Upvotes: 3

Related Questions