Question

I keep getting this error:

The operator != and == is undefined for the argument type(s) boolean, int

on my code and I have no idea on what to do to fix it. Help would be greatly appreciated :) Thanks!

public static boolean isPressed(int i)
{
  return (keyState[i] != 0) && (prevKeyState[i] == 0);
}
Was it helpful?

Solution

It looks like keyState[i] and prevKeyState[i] are booleans (true/false values), so it makes no sense to compare them to zero. Maybe what you want is

return (keyState[i] == true) && (prevKeyState[i] == false);

or even, in a more readable form

return keyState[i]  &&  ! prevKeyState[i];

OTHER TIPS

You should do:

return (keyState[i] == true) && (prevKeyState[i] == false);

Because keystate[i] and prevkeystate[i] are of type bool, you must also compare them with a bool. Alternatively you could write:

return keyState[i] && !prevKeyState[i];

You are comparing a boolean with an int. Are keyState and prevKeyState both of type boolean[]? If so, this might explain why you are seeing this error.

Simply change your statement to:

return (keyState[i] && !prevKeyState[i]);

and it should fix the issue.

As the error message says, you cannot use != to compare a boolean on the left-hand side with an integer (the 0) on the right-hand side. Judging from the error message, it sounds like keyState and prevKeyState are both boolean[].

So to fix this, either change your arrays to be int[] or test for keyState[i] != false instead of keyState[i] != 0.

Unlike C, Java has a boolean type and you shouldn't use integers like 1 and 0 to stand in for true / false.

just read the error message from the compiler:

keyState and prevKeyState are surely of type: boolean[]

your code must look like this:

public static boolean isPressed(int i)
{
  return (keyState[i]) && (! prevKeyState[i]);
}

I think you've declared boolean arrays boolean[] keyState and boolean[]prevKeyState, but you haven't made that clear here.

If this is true, then you are comparing boolean values keyState[i] and prevKeyState[i] to int values 0 and 1.

try this:

return (keyState[i] && !prevKeyState[i]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top