Forums Register Login

Output a letter for a number entered

+Pie Number of slices to send: Send
I am working on my HW and I am stuck at a part.

I have to accept input from scanner and output something different depending on the number entered at console.

I have all but one part done.

If the number entered is 10, I output the letter A, 11 = B, 12 = C... to Z

I am supposed to use cast operator (char) to do this, but I did not find it in the reading and it is buried in the java API doc.

Can I have a life line?
+Pie Number of slices to send: Send
//55 because the upper case alphabet letters start from 65 in ASCI table


char alpha= (char)(55+Integer.parseInt(num));
+Pie Number of slices to send: Send
 

binayakumar patel wrote://55 because the upper case alphabet letters start from 65 in ASCI table


char alpha= (char)(55+Integer.parseInt(num));



Or you can try to make the code more idiot-proof. Shoot, you don't even have to know that 'A' corresponds to 65 since you can let the program worry about that for you. If I know that 10 will correspond to 'A', I'll put that in my code:
+Pie Number of slices to send: Send
Even better . . . though you will have to edit and enhance this code, and I have left bits out:
+Pie Number of slices to send: Send
personally, if i were inputting the offset and want A = 11, I'd expect to input '11'. but that's just me.
+Pie Number of slices to send: Send
I take your point, Fred. That could be done by putting + 1 somewhere.
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1394 times.
Similar Threads
Random / swtich operator
while using System.in read()?
validating forms
JAVA Code, doesnt work !
sorting an array
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:22:15.