Josh Morrison
Josh Morrison

Reputation: 7636

How can I do this int to String transformation?

I want to transform an int to a String such that:

0 -> "a"
1 -> "b"
2 -> "c"
and so on...

How can I do this?

Upvotes: 0

Views: 220

Answers (3)

David Tang
David Tang

Reputation: 93664

You can convert from the character literal:

int input = 0;
String output = new Character((char) (input + 'a')).toString();

Upvotes: 3

robert_x44
robert_x44

Reputation: 9314

An alternate method, for some java library flavor:

int value;
String output = Integer.toString(value + 10, 36);

which uses a radix of 36 to locate the right letter.

Upvotes: 0

David
David

Reputation: 218828

Your question is a little unclear, but it sounds like you want to be able to convert integers 0-25 to their corresponding alphabetical characters. If that's the case, your best bet logically is probably to use an enum. Though I may not be fully seeing the purpose of what you're trying to do (which is likely).

You could also write a utility method which just has a big switch statement to convert them.

Upvotes: 0

Related Questions