...

View Full Version : Casting char into int



kurtulas
02-15-2008, 09:46 AM
Hi,
Any help with this would be appreciated.

I have created a method which will generate a random number using 'nextInt()' and imported 'java.util*;'. I need to cast this random number into an char but i can't seem to get it to work, my code in the method is:
char ch; int n; ch = (char); n = ch; return noise.nextInt(25) + 97;

Dont worry about 'noise' its just a variable i have assigned to the class.

Thanks alot, kurt.

Philip M
02-15-2008, 10:46 AM
This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

I googled and found:-
http://privacy.cs.cmu.edu/courses/java2/lectures/lecture16/sld075.htm

Jero
02-15-2008, 05:25 PM
This is how you cast an int to a char:


int number = 65;
char character = (char) number;

So in order to get your code to work, change it to the following:


return (char) (noise.nextInt(25) + 97);

Also, make sure your return type is set to char.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum