Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

alphabet equal to numbers?

 
Kanika Bansal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way in java by which we can associate each number of the alphabet with a number directly? like a=1, b=2 , c=3 and so on.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

You don't need to associate numbers; they are there already. If you go to Unicode, you find there already are numbers. In fact the computer doesn't store 'a'; it stores 0x61 (97 in decimal).
You can use % 0x20 after 'a', or 'a' - 'a' + 1. Both will work, but will give very peculiar results for things which aren't letters. They produce an int result.
 
Kanika Bansal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well dats ok but i m not looking for dat. actually i need to do a program in which i have to see dat if a char = a, then it reads d value stored at 0 in an array. similarly if it is b, then from position 1 and so on till z.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea what 'dats', 'm', or 'd' means. Data? monkey? dictation?

We really prefer folks to use real words here. Many of our visitors don't have English as a first language, so using shorthand like this makes understanding complex topics even MORE difficult.

Why do you need the value for 'a' to be in the 0th element in the array? Memory is cheap. Why not have an array with, say, 127 elements, and then just not use most of them? 'a' would refer to element 97, 'b' to 98, etc., with elements 0-96 emtpy.
 
Henry Wong
author
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kanika Bansal:
well [th]ats ok but i [a]m not looking for [th]at. actually i need to do a program in which i have to see [th]at if a char = a, then it reads [the] value stored at 0 in an array. similarly if it is b, then from position 1 and so on till z.


Unicode for the alphabet is linear. So... if you convert the letter to unicode, then subtract the unicode for "a", you will get zero for "a", one for "b", two for "c", etc.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic