Results 1 to 3 of 3
Thread: Casting an int value into a char
- 02-16-2008, 12:44 PM #1
Member
- Join Date
- Feb 2008
- Posts
- 2
- Rep Power
- 0
Casting an int value into a char
Hi all;
This is my first post here. Been working with java for over a year now so im pretty new but ill help others where i can. Here is my first question.
I have a method that creates a random number (so the number is never known until the method runs), i have been trying for days to cast this number into a char. I have written various ways but after succesful compiling i still get a number rather than a char. Any help please???
- 02-16-2008, 05:31 PM #2
Member
- Join Date
- Dec 2007
- Posts
- 30
- Rep Power
- 0
I don't know if you can directly cast an int to a char but, if the range of your numbers is small, you could just use a switch statement and another method to get the char you want... like this:
switch (x) { //x is the int
case 1: return ('a');
case 2: return ('b');
}Last edited by Leprechaun; 02-16-2008 at 05:32 PM. Reason: Add {} to the code for the switch statement
- 02-16-2008, 08:03 PM #3
Member
- Join Date
- Jan 2008
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Type Casting Help
By rhm54 in forum New To JavaReplies: 2Last Post: 02-07-2008, 12:06 PM -
'Casting' couch !!!!
By ajaygargnsit in forum Advanced JavaReplies: 4Last Post: 01-04-2008, 04:54 PM -
'Casting' couch !!
By ajaygargnsit in forum New To JavaReplies: 1Last Post: 12-22-2007, 01:05 PM -
Casting
By leebee in forum New To JavaReplies: 5Last Post: 08-10-2007, 12:24 PM -
Help with, String, Char
By lenny in forum New To JavaReplies: 1Last Post: 07-25-2007, 02:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks