My answer is similar to jh314's answer, but I will explain a little deeper.
What you should do in this case:
int a = 1; char b = (char)(a + '0'); System.out.println(b);
Here we used '0' because the characters are actually represented by ASCII values. "0" is the character represented by 48.
We typed (a + '0') and, to add them, Java converted '0' to its ASCII value, which is 48, and a is 1, so the sum is 49. Then we did the following:
(char)(49)
We threw int on char . ASCII, equivalent to 49, is "1". You can convert any digit to a character this way, and it will be smarter and better than using the .toString() method and then subtracting the digit with the .charAt() method.
Haggra Jun 04 '15 at 21:05 2015-06-04 21:05
source share