Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-16-2008, 01:44 PM
Member
 
Join Date: Feb 2008
Posts: 2
Rep Power: 0
kurtulas is on a distinguished road
Default 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???
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-16-2008, 06:31 PM
Member
 
Join Date: Dec 2007
Posts: 30
Rep Power: 0
Leprechaun is on a distinguished road
Default
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:
Quote:
switch (x) { //x is the int
case 1: return ('a');
case 2: return ('b');
}

Last edited by Leprechaun; 02-16-2008 at 06:32 PM. Reason: Add {} to the code for the switch statement
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-16-2008, 09:03 PM
Member
 
Join Date: Jan 2008
Location: South Africa
Posts: 18
Rep Power: 0
jimm1 has a little shameless behaviour in the past
Default
Its very easy:
char c = 112; (Or whatever number you want)

OR

char c = (char)112;

System.out.println(c);

The output will be 'p';
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type Casting Help rhm54 New To Java 2 02-07-2008 01:06 PM
'Casting' couch !!!! ajaygargnsit Advanced Java 4 01-04-2008 05:54 PM
'Casting' couch !! ajaygargnsit New To Java 1 12-22-2007 02:05 PM
Casting leebee New To Java 5 08-10-2007 01:24 PM
Help with, String, Char lenny New To Java 1 07-25-2007 03:58 PM


All times are GMT +2. The time now is 01:36 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org