Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Casting char into int

    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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/ja...e16/sld075.htm
    Last edited by Philip M; 02-15-2008 at 09:49 AM.

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    211
    Thanks
    0
    Thanked 1 Time in 1 Post
    This is how you cast an int to a char:

    Code:
    int number = 65;
    char character = (char) number;
    So in order to get your code to work, change it to the following:

    Code:
    return (char) (noise.nextInt(25) + 97);
    Also, make sure your return type is set to char.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •