Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map character to numeric position in java?

Tags:

java

E.g.

  • input: ['A', 'Z', 'F', 'D', ...]
  • output: [0, 25, 5, 3, ...]

In C I'd just subtract the char from 'A', but I don't seem to be able to do this in java.

like image 719
Alex Budovski Avatar asked Apr 02 '10 01:04

Alex Budovski


People also ask

How do you map a character in Java?

Declare a Hashmap in Java of {char, int}. Traverse in the string, check if the Hashmap already contains the traversed character or not. If it is present, then increase its count using get() and put() function in Hashmap. Once the traversal is completed, traverse in the Hashmap and print the character and its frequency.

How do you add a character to an integer in Java?

In Java, char and int are compatible types so just add them with + operator. c + x results in an integer, so you need an explicit casting to assign it to your character varaible back.

How do I find the position of the alphabet?

So to find this there is a very easy way. Let us look at it. To get it we can find the ASCII value of the alphabet and then we can subtract 64 from it for the uppercase Alphabet and for lowercase we need to subtract 96 from it. Or First, you can convert the string to upper or lower case and then can find it.


3 Answers

Use the indexOf method on a String object. For example,

"ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf('F')

returns 5.

like image 89
Etaoin Avatar answered Oct 20 '22 15:10

Etaoin


You can do simple math with chars in Java as well:

    System.out.println('A' - 'A');

will output 0.

like image 41
jarnbjo Avatar answered Oct 20 '22 15:10

jarnbjo


actually the weak point of the other solutions here is that they involve string creation

public enum Alphabet {
    A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
}

you can now use the ordinal function to get the offset in here. e.g. Alphabet.L.ordinal();

However, since I assume you are dealing with functions, here is a more useful definition

public enum Alphabet {
    A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z;

    public static int getNum(String targ) {
        return valueOf(targ).ordinal();
    }

    public static int getNum(char targ) {
        return valueOf(String.valueOf(targ)).ordinal();
    }    
}

Notes: unlike other languages, you can declare an enum in it's own file exactly like a class. Actually enums as shown above can contain fields and methods too, the fields are statically created, and are very hard to break. In fact the use of an enum with only local methods and variables and a single enum type called INSTANCE is the recommended way to create a singleton as it is unbreakable even by reflection.

You may want to think about slipping a toUppercase() call in there too if you are not controlling the calls to the function

If you are looking to more dynamically create your alphabet rather than use a predefined alphabet, you should be looking into maps

like image 44
K.Barad Avatar answered Oct 20 '22 14:10

K.Barad