0

In Java, I need to be able to get the subscript value of an array item. Say in a sting array, letters[25] stores the letter Z. If my user inputs a Z, I need to get that subscript value 25 as an integer (so I can plug it in later in another array that contains other correlated data).

My array letters[] has 26 elements, basically A through Z such that letters[0] = A and so on. I have another array containing integer values (also 26 total elements). Trying to convert letters to number values.

4
  • 2
    Why are you not using a Hashmap? Commented Jun 18, 2017 at 13:31
  • Possible duplicate of Java - how to convert letters in a string to a number? Commented Jun 18, 2017 at 13:32
  • Class java.util.Arrays has a lot of binarySearch methods. Commented Jun 18, 2017 at 13:33
  • Yeah... I was thinking the binarySearch methods might hold the key. Still working on it. As to why I am using basically a hash, well, I need to calculate based on the number value of letters that themselves can change depending on another part of the program. Thus making an array for just the letters and another for the variable integer values sort of made sense. I looked at hat other thread and the map option looks interesting but I am still thinking two arrays gives me the flexibility I need. Commented Jun 18, 2017 at 14:27

3 Answers 3

1

A character can be converted to a number by getting its ASCII code.

int ascii = (int) character;

Assume your characters are in lowercase, you need then to subtract 97 from the result integer which will give you the index of the character in your letters array.

Sign up to request clarification or add additional context in comments.

Comments

0

To find an index of an element in an array, you can use a binary search:

//First, sort the array:
java.util.Arrays.sort(letters);
int indexOfZ = java.util.Arrays.binarySearch(letters, "Z");

Please check the JavaDocs to see what to expect when the value is not found (it will be a negative int).

3 Comments

But then you don't have the index in the original array unless the sort method would give you an index map array.
True. But sorting should not be necessary, as the array is already sorted as per the question. It's just a note that the array must be sorted to use binary search.
Well... That was an abundance of options! Thanks I think I can figure it out from here. Basically taking a 'word' and then taking each letter thereof as a separate variable. Got most of the code done using an if loop and feeding an incrementing charAt into an array up to that 'word' length(). Sort of doing an analogy for genetics (hard to explain). Trying to work only off the standard libraries. Good point about converting to lowercase first too to avoid negative number errors from invalid references to my limited array. That would have otherwise lead to some bizarre calculation results.
0

Simplest way (one of many) to do this is use String, it already have internal array. You need to have this code:

 public int indexOfLetter(String letter) {
    String alphabet = "abcdefghijklmnopqrstuvwxyz";
    int index = alphabet.indexOf(letter.toLowerCase());
    if (index == -1) {
        throw new RuntimeException("letter not found");
    }
    return index;
}

example:

System.out.println(indexOfLetter("z")); // 25
System.out.println(indexOfLetter("Z")); // 25

3 Comments

What is the + 1 for?
@glglgl array indexation from 0, and letter indexation in alphabet starts from 1, if we select "a" letter we get 0 index instead expected 1
The OP states "If my user inputs a Z, I need to get that subscript value 25 as an integer". No word about that 1 is expected for A.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.