Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Converting Int to Char
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prince Pwn




PostPosted: Mon May 28, 2007 10:33 am   Post subject: Converting Int to Char

I need to convert an Integer into a Character so I could use a for loop to assign a number to a variable when I press a 1 2 3 or 4. For some reason it isn't reading the for loop input.

I'm basically needing a way to automate:
Java:

            if (key == '1')
                amount = 1;
            else if (key == '2')
                amount = 2;
            else if (key == '3')
                amount = 3;
            else if (key == '4')
                amount = 4;


Here is my code:
Java:

    public boolean keyDown (Event moving, int key)
    {

        if (introScreen)
        {
            if (key == Event.ESCAPE)
                System.exit (0);
            if (key == Event.RIGHT)
                amount++; // WORKS!
            else if (key == Event.LEFT)
                amount--; // WORKS!

            for (int i = 1 ; i <= maxBall ; i++) // counts to 4
            {
                if (key == char (i))  // if i press any number, 1 - 4 // DOESNT WORK!
                    amount = i;    // amount becomes equal to the number I press
            }

            if (key == Event.ENTER)
                introScreen = false;
        }
Sponsor
Sponsor
Sponsor
sponsor
HeavenAgain




PostPosted: Mon May 28, 2007 9:19 pm   Post subject: Re: Converting Int to Char

hello, the problem should be

if (key == char (i))

what do you mean by char (i) ? trying to cast an int to a char ? then it should be
code:

(char)(12)

since i is in the for loop, i dont get the part where you enter a number? but anyways
converting a String using java the code is
code:

Integer.parseInt();

example
code:

Integer.parseInt("100");

that would give you 100 as integer

by the way,i suggest you use another variable for this
Laughing Laughing i hope this helps?
OneOffDriveByPoster




PostPosted: Tue May 29, 2007 8:03 am   Post subject: Re: Converting Int to Char

Prince Pwn @ Mon May 28, 2007 10:33 am wrote:
Java:
            for (int i = 1 ; i <= maxBall ; i++) // counts to 4
            {
                if (key == char (i))  // if i press any number, 1 - 4 // DOESNT WORK!
                    amount = i;    // amount becomes equal to the number I press
            }

Consider this: (char)0x30 is '0'.

So your conversion should be (char)(i | 0x30)--this is okay because you control the value of "i"; this will not work with # > 9.
You can also check out Java characters and their relation to Unicode.
Prince Pwn




PostPosted: Wed May 30, 2007 6:01 pm   Post subject: Re: Converting Int to Char

Thankyou.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: