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

asked Nov 5, 2014 at 18:48
5
  • 3
    Show us the complete code and problem Commented Nov 5, 2014 at 18:49
  • 2
    What error are you getting? Commented Nov 5, 2014 at 18:49
  • 1
    ... and the error, please Commented Nov 5, 2014 at 18:49
  • Possible errors just by looking at the code - answers out of scope, array out of bounds, etc. 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円') {
 ...
}
answered Nov 5, 2014 at 18:53
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!");
 }
 }
}
answered Nov 5, 2014 at 18:52
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.");
}
answered Nov 5, 2014 at 19:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.