Thursday, July 30, 2009

How can i use letters and numbers in a java program?

I want to write a java program that will guess numbers and letters. I want the program to guess what number or letter it is by giving a random letter or number. For instance I am using 1 2 or 3 and a b c. I can get them to work if I do one at a time but I can not get them both in a program together. I tried to cast a char to an int but could not get that to work either. Any ideas on how to get both letters and numbers into the same program?

How can i use letters and numbers in a java program?
Assuming i got what you mean, but if that was wrong, you may have to provide us with some more details, please.





Now to your question...


To do so, just use this simple trick:





     01    int x = 5;


     02    char y = 'x';


     03    String z = new String();


     04


     05    z = x + "" + y; //note those "" here ? //z equ. "5x"





Note that it will not make any difference if both of them is characters; i.e. you can use that trick as a general method to add two characters/integers together and forming a new string of them.





Thanks.
Reply:You have two choices, treat everything as Strings or chars. Casting a char to an int will give you its ascii value, so it is unlikely this is what you want.


No comments:

Post a Comment