Hi I'm trying to subtract an int from a char but I keep being told that the compiler "cannot convert from int to char". I have tried changing the constant to a char but it didn't help.
is there any easy way to do this subtraction?
test[1] = characterArray[1] - ASCII_SUB;
Any help much would be appreciated.
The problem is that subtraction is never performed on char
values in Java. Instead, both operands are promoted to int
(via binary numeric promotion), and the result of the subtraction is an int
as well. So you'll need to cast the result back to char
:
test[1] = (char) (characterArray[1] - ASCII_SUB);
See more on this question at Stackoverflow