Good day, I am fairly new to Java and is wondering how do I give letters its corresponding int value like the image below. Currently stuck on this for a project I am doing. For example, when user inputs ABC, the output is 321, since it requires it to be reverse.
-
The answer to the question in the title is `char c = (int) someIntVariable;`. The rest of your question is to unclear and (probably) too broad. – Stephen C Jan 17 '21 at 02:22
-
1Please read [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – Stephen C Jan 17 '21 at 02:23
-
1This is not a homework-writing service. Please read http://stackoverflow.com/help – NomadMaker Jan 17 '21 at 02:26
-
To convert an ascii capital letter to an int, ``int a = (int)(c - 'A' + 1);`` – NomadMaker Jan 17 '21 at 02:28
5 Answers
This would work :
public class Test {
public static void main(String args[]) throws Exception {
String str = "ABC";
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray())
sb.append((int)c-64);
System.out.println(sb.reverse());
}
}
- 10,528
- 6
- 54
- 53
Loop Unicode code points
Get the Unicode code point number of each character in the string.
Get the count of code points by calling String#codePointCount.
Then loop each code point by calling String#codePointAt. Notice that we must use annoying zero-based counting rather than ordinal numbers, an all-too-common and unfortunate hold-over habit from the earliest days of programming.
String input = "Hi" ;
int countCodePoints = input.codePointCount( 0 , input.length() ) ;
for( int i = 0 ; i < countCodePoints ; i ++ )
{
System.out.println( "i: " + i + " | " + input.codePointAt( i ) ) ;
}
See this code run live at IdeOne.com.
i: 0 | 72
i: 1 | 105
i: 2 | 128512
Avoid char
Other Answers on this page unfortunately use the char type. That type is now legacy, dating back to original versions of Java.
That type is obsolete because it cannot address even half of the 143,859 characters defined in Unicode. For fun, run this code to see what happens when we use char type to look at the same input string seen above: "Hi".chars().forEach( c -> System.out.println( c ) ) .
Your solution
Back to your specific example of input text:
String input = "ABC" ;
int countCodePoints = input.codePointCount( 0 , input.length() ) ;
for( int i = 0 ; i < countCodePoints ; i ++ )
{
System.out.println( "i: " + i + " | " + input.codePointAt( i ) ) ;
}
When run:
i: 0 | 65
i: 1 | 66
i: 2 | 67
Since this is homework, I will leave to you the work of subtracting and reversing.
Additional reading: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!).
- 303,325
- 100
- 852
- 1,154
Iterate over each character (in reverse) and attain the ASCII value (reference this question: https://stackoverflow.com/a/16458580). Then subtract its value based on ASCII (reference this table: https://www.ascii-code.com/). I would recommend sanitizing the input so that letters are either only uppercase or lowercase (depends on your requirements however).
- 147
- 2
- 10
The easiest/quickest way is using a switch statement. For example,
public static int charToInt(char c) {
switch(c) {
case 'A':
return 1;
case 'B':
return 2;
//...
}
}
You can make a similar function for the reverse with a method prototype similar to this:
public static char intToChar(int i)
To convert a String to an array of characters (char[]) use the method String.toCharArray. Once you have your char[] arr, use arr.length to get the size of the array. Use that size to create an integer array (int[]) with the same length, and you can loop through the character array and convert them to integers using your custom conversion method.
- 68
- 4
I would suggest Map<Character, Integer>. You can assign Integer values to Characters and you can always get it with the simple getValue() method from the Map interface.
- 78
- 2
- 18