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
3 Answers 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
toInteger
- This would let you check its values fornull
, 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 ofanswer[]
'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円') {
...
}
-
1Great answer. Thanks for the help and being specific with each answer.Lewis Menelaws– Lewis Menelaws2014年11月05日 19:06:32 +00:00Commented Nov 5, 2014 at 19:06
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!");
}
}
}
-
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.Lewis Menelaws– Lewis Menelaws2014年11月05日 18:55:10 +00:00Commented 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!Am_I_Helpful– Am_I_Helpful2014年11月05日 19:04:58 +00:00Commented 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.Am_I_Helpful– Am_I_Helpful2014年11月05日 19:05:55 +00:00Commented Nov 5, 2014 at 19:05
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.");
}
answers
out of scope, array out of bounds, etc.