ProgrammingBear
ProgrammingBear

Reputation: 23

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?

Upvotes: 0

Views: 48

Answers (2)

Michail Alexakis
Michail Alexakis

Reputation: 1595

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

Upvotes: 0

m_dot_dev
m_dot_dev

Reputation: 65

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

Upvotes: 2

Related Questions