'Console prints the char's ASCII Value, not the sign itself
I have variable:
char characterA = '#';
int arr = new arr[5][5];
arr[3][1] = characterA;
When I print the array it gives me its value, not the char itself. How can I show the char?
Solution 1:[1]
One way is to use a char[][] array instead of an int[][] array. Since you are using an int array. java converts the character into it's ascii value and stores that
Solution 2:[2]
If you have to store your char as an int (e.g if the array is created elsewere), you still can print it as char using the printf family of methods (String.format, PrintStream.printf etc) and modifier %c.
For example:
char c = 'A';
int n = c;
System.err.printf("%c", n); // prints 'A'
The full documentation on formatting syntax is here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Muhammad Talha |
| Solution 2 | Michail Alexakis |
