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?
CodePudding user response:
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
CodePudding user response:
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