Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-16-2008, 01:44 PM
Member
 
Join Date: Feb 2008
Posts: 2
kurtulas is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 02-16-2008, 06:31 PM
Member
 
Join Date: Dec 2007
Posts: 30
Leprechaun is on a distinguished road
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
jimm1 has a little shameless behaviour in the past
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
Sponsored Links
Reply


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

vB 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 +3. The time now is 11:50 AM.


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