How to make charAt() return a number you can fill in an array?

Magnus2142 :

I got a task where the user writes in a string, and the a constructor will use charAt() and length() to fill in in an array how many times each letter occur.

My problem is that I don't know how to make the charAt() method return a value I can fill into the array.

Here is what I have so far. I'm really new to coding.

public class TekstVerktøy{
    private String text;
    private int[] letterArray = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

    /* Konstruktør */
    public TekstVerktøy(String t){
        int i = 0;
        letterArray = letterArray;
        this.text = t;

        while(i <= this.text.length()){
            i = i + 1;
        }
    }
}
T.J. Crowder :

charAt returns a char, which is the UTF-16 code unit in the Java string at the position you requested. For instance, 'A' is the char whose int value is 65 ('B' is 66 and so on); 'a' is the char whose int value is 97 ('b' is 98 and so on); etc. You can convert a char to an int by simply assigning it to an int variable or using it in any int context (such as indexing into an array or as part of a subtraction expression) or via an explicit cast:

int codeUnit = this.text.charAt(i);

Then you need to do some math on the result to determine the correct index for your array, and increment the value there.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=140500&siteId=1