-8

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.

this image here

Lex O.
  • 1
  • 1

5 Answers5

1

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());
    }


}
Joby Wilson Mathews
  • 10,528
  • 6
  • 54
  • 53
1

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!).

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

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).

WebSpence
  • 147
  • 2
  • 10
0

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.

-1

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.