I am looking to convert numbers to their corresponding letter in the alphabet.
Asked
Active
Viewed 4,566 times
-1
-
[Try this Post](http://stackoverflow.com/questions/5328996/java-change-int-to-ascii) Question was already asked to stackoverflow – Lukas A Apr 10 '17 at 14:28
-
The term alphabet is not very precise and the system of a number's corresponding letter is unclear. I am familiar with the English alphabet of 26 letters from to A-Z and the idea of numbering them 1 to 26. However, Java uses Unicode for text datatypes. Unicode has at least two forms for those letters (lowercase and uppercase) as well as a number of specialized scripts with some or all of those letters. It also has its own [numbering system](http://www.unicode.org/charts/nameslist/index.html) for every character (usually written in hexadecimal like U+0041). Is that what you mean? – Tom Blodget Apr 10 '17 at 18:21
3 Answers
3
As seen here alphabetical characters start from either 65 or 97 (depending on capitalization) so you just add either 64 or 96 to your value and then just cast it to a char.
Random rand = new Random();
int TargetNumber = rand.nextInt(25) + 1;
char TargetChar = (char) (TargetNumber+64);

Nasto
- 428
- 2
- 11
-
I want the corresponding lowercase letter so i add 96? Thanks for the help! – java1234 Apr 10 '17 at 14:38
-
yes, `char TargetChar = (char) (TargetNumber+96);` will give you the lowercase char – Nasto Apr 10 '17 at 14:51
0
Try this
Random rand = new Random();
int TargetNumber = rand.nextInt(25) + 1;
char c = (char)(TargetNumber+96);
This adds 96
to the generated value, and then type casts it into a char
. This works for a-z
. To make it through A-Z
replace 96
, by 64
.

dumbPotato21
- 5,669
- 5
- 21
- 34