Search code examples
javacharacterintegral

convert integer to a character


I am having a problem with my java code, basically I want to do this;

char letter = 'a';
char convertedletter = letter + 5;
System.out.print(convertedletter);

the output should be f, but the output I get is 108. How could I make it an f?


Solution

  • try this:

    char letter = 'a';
    char convertedletter;
    convertedletter = (char) (letter + 5);
    System.out.print(convertedletter);
    

    The operator "+" is used to add numbers or concatenate String, since you are using it whit a char (only one character) and a int it would return a int unless you converted it to char with is what you want.