• Congratulations to the Class of 2024 on your results!
    Let us know how you went here
    Got a question about your uni preferences? Ask us here

Java: int to char (1 Viewer)

acmilan

I'll stab ya
Joined
May 24, 2004
Messages
3,989
Location
Jumanji
Gender
Male
HSC
N/A
Hi, im having a little trouble converting int to char in java :/

What i know works:

Code:
String str = Integer.toString(i);
char c = str.charAt(0);
Is there any better way though than creating a string?

I thought maybe:

Code:
int i = 0;
char c = (char)i;
might work, but it just prints a smiley face

Thanks!
 

acmilan

I'll stab ya
Joined
May 24, 2004
Messages
3,989
Location
Jumanji
Gender
Male
HSC
N/A
nevermind, solved!

Code:
int i = 0;
char c = (char)(i + '0');
works
 

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
acmilan said:
nevermind, solved!

Code:
int i = 0;
char c = (char)(i + '0');
works
What exactly are you trying to do?? Even though it might be working for you, I'm confused why you say this is the solution...
 

acmilan

I'll stab ya
Joined
May 24, 2004
Messages
3,989
Location
Jumanji
Gender
Male
HSC
N/A
sunny said:
What exactly are you trying to do?? Even though it might be working for you, I'm confused why you say this is the solution...
trying to store numbers in a char array
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top