2

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

5
  • 3
    Show us the complete code and problem Commented Nov 5, 2014 at 18:49
  • 2
    What error are you getting?
    – Eran
    Commented Nov 5, 2014 at 18:49
  • 1
    ... and the error, please
    – Mureinik
    Commented Nov 5, 2014 at 18:49
  • Possible errors just by looking at the code - answers out of scope, array out of bounds, etc.
    – Compass
    Commented Nov 5, 2014 at 18:51
  • Sorry. Added all of the code and the error message. Commented Nov 5, 2014 at 18:53

3 Answers 3

3

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') {
    ...
}
1
  • 1
    Great answer. Thanks for the help and being specific with each answer. Commented Nov 5, 2014 at 19:06
2

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!");
        }
    }
}
3
  • Thank you for the answer. However, if I use == 0 doesn't that mean it wouldn't return anything or do most arrays have a default value of 0? Thank you. Commented Nov 5, 2014 at 18:55
  • @Lewis-SORRY Lewis,edited my answer. In java,array members are always initialised by 0 whatever be the case so consider -1 for sentinel condition! Commented Nov 5, 2014 at 19:04
  • @dasblinkenlight-THANKS,corrected. I was confused between C and Java as nowadays I am busy with some C coding!!! THANKS and +! for you. Also, I mentioned your point in my answer. Commented Nov 5, 2014 at 19:05
1

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.");
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.