0

I have this project that requires me to count the occurrences of each character inputted in the JTextArea and output each character with how many times it occurred into another text box by clicking a button. My code only works for the last character of the string, it displays and counts that specific character but not the rest of the characters in the string. I need my code to count all of the characters and output it. Can someone help.

Here's my code:

btnStat.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
        String text = textbox1.getText();
        int counted[] = new int[MAX_CHAR];
        int sizes = text.length();
        int find = 0;
        char[] cat = new char[text.length()];

            for (int i = 0; i < sizes; i++) {
                cat[i] = text.charAt(i);
                for (int j = 0; j <= i; j++) {
                    if (text.charAt(i) == cat[j]) {
                        find++;
                    }
                    textbox6.setText("Character Frequency: \n '" + text.charAt(j) + "' - " + counted[text.charAt(j)] + " ,");
                }
            }
        }
}
blkpanda
  • 1
  • 1
  • Well, what do you think `textbox6.setText(...)` does to the current content of that textbox? Have you checked the corresponding JavaDoc? Use `append()` instead. – Tom May 27 '21 at 23:24
  • Sorry I edited it, there was supposed to be only two for loops – blkpanda May 27 '21 at 23:26

0 Answers0