+ 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.

28th Feb 2020, 5:26 AM
Karthikeyan
Karthikeyan - avatar
7 odpowiedzi
+ 4
char variables only hold a single character either 1 or 7 which one you want it to hold?
28th Feb 2020, 5:31 AM
Archisio
Archisio - avatar
0
7
28th Feb 2020, 5:32 AM
Karthikeyan
Karthikeyan - avatar
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.
28th Feb 2020, 5:39 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
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
28th Feb 2020, 5:40 AM
Shen Bapiro
Shen Bapiro - avatar
0
First convert int to string: Integer.toString() then .charAt() As above: char c = Integer.toString().charAt(1)
28th Feb 2020, 5:40 AM
Archisio
Archisio - avatar
0
Thankyou guys
28th Feb 2020, 7:09 AM
Karthikeyan
Karthikeyan - avatar
- 1
char c = 17; System.out.print((int)c);
28th Feb 2020, 5:35 AM
Avinesh
Avinesh - avatar