Reputation: 431
I am trying to make a program that concatenate characters cast from ints into Strings and then puts those strings into an array and prints them. I have tried a few different ways to make characters into string including Character.toString() ,StringBuilders and adding "". Yet,whenever I print to the console in eclipse I just get question marks instead of letters. How can I solve this. Here is my code:
public static void main(String args[]){
try{
char b = (char)2;
String god = Character.toString(b);
System.out.println(god);
}
Upvotes: 1
Views: 282
Reputation: 476
Casting an int to a char in Java will use the Ascii code that int represents.
The Ascii code 2 is not a printable character, hence why you are getting question marks. You can see a list of Ascii codes here
I presume what you wanted to do is use '2' instead. (Note the quotation marks.)
char b = '2';
Upvotes: 2