+ 1
In java how to assign int value to char
Int i=17; Char c=(char)i; If I do like above I get c value as ?. But I need c value as 17.
7 odpowiedzi
+ 4
char variables only hold a single character either 1 or 7 which one you want it to hold?
0
7
0
May I know why you want to store an integer in a char?
Java has several primitive datatypes as well as Classes.
Why don't you simply use a string?
String s = "17";
or
String s = String.valueOf(17);
You can also typecast the values.
char c = (char)17;
System.out.println((int) c);
But why bother? Just use String.
0
go to www.asciitable.com
look and notice that ascii number 17 when turned into char is called device control 1 which is not printable and not a visible char.
so try using an ascii that can be turned into a char such as 97.
int i = 97;
char c = (char)ascii;
System.out.println(c)
>>>
a
0
First convert int to string: Integer.toString()
then
.charAt()
As above:
char c = Integer.toString().charAt(1)
0
Thankyou guys
- 1
char c = 17;
System.out.print((int)c);