Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: converting int to char

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default converting int to char

    I just want to convert an integer...say 8....to a char...which will be '8'.

    int z = 5;
    char e;
     
    e = Character.forDigit(z,10);
     
    //e should become '5'

    This is really weird...when i use the debugger the value of e will be correct...other times it will be blank...is there another way to do this conversion?
    Last edited by helloworld922; February 18th, 2011 at 08:58 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: converting int to char

    It should always be correct. Make sure you're at a point while debugging that e is indeed '5' (or whatever character you want).

    int z = 5;
    char e; // e is nothing
     
    e = Character.forDigit(z,10); // is is still nothing at beginning of this statement, becomes '5' at the end of this statement
    System.out.println(e); // e is still '5'

    Also, is your code taken from somewhere else? Perhaps the problem lies in where you're trying to use forDigit()?

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: converting int to char

    I'm not going to throw my code up here...but I am re-initializing the same char variable multiple times.

    The first time......e = Character.forDigit(z,10);....gets executed.....e is what it should be


    The second time....e = Character.forDigit(z,10);.....gets executed....e simply becomes blank or ' '....doesn't make any sense...and this is the last part to finish the program.


    I actually tried using another variable in replace of the second execution and it still doesnt work...it has me stumped.
    Last edited by surfbumb; February 19th, 2011 at 01:13 AM.

Similar Threads

  1. char.toHexString();
    By SPACE MONKEY in forum What's Wrong With My Code?
    Replies: 16
    Last Post: February 28th, 2011, 08:46 AM
  2. [SOLVED] Why do i need to take 48? char to int.
    By Scotty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 29th, 2010, 10:23 PM
  3. Char cannot be dereferenced!! Please help
    By humdinger in forum Object Oriented Programming
    Replies: 5
    Last Post: February 14th, 2010, 03:18 PM
  4. char (toUppercase?)
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 12th, 2009, 10:01 AM
  5. Program to convert Hexadecimal to its Character equivalent
    By nathanernest in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2009, 03:12 AM