0

I'm just learning that you can add an int to a char. I've tried out the following expecting it to compile, but I'm getting an incompatible types error:

char a = 'e' + (number / 10)

I can't figure out why, if

char c = '1' 

I've seen that similar questions suggest the use of 'final' to resolve, but I've modified it to apply that and it still gets the same error.....

works then the above doesn't....

Any ideas?

javapalava
  • 711
  • 1
  • 6
  • 15

1 Answers1

1

This occurs because you need to cast the chars into ints before you can actually perform calculations with them: int answer = (int) 'e' + (number / 10) And then cast the answer back to a char:

answer = (char) answer
Ittociwam
  • 91
  • 2
  • 8