• Post Reply Bookmark Topic Watch Topic
  • New Topic

Output a letter for a number entered  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//55 because the upper case alphabet letters start from 65 in ASCI table


char alpha= (char)(55+Integer.parseInt(num));
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even better . . . though you will have to edit and enhance this code, and I have left bits out:
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
personally, if i were inputting the offset and want A = 11, I'd expect to input '11'. but that's just me.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I take your point, Fred. That could be done by putting + 1 somewhere.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!