Java Help Please:( It is not working

/*
 *Write a Java program/class which creates an array with 100 random letters *using characters from ‘A’ to ‘Z’, displays the array in a 10x10 format, and displays *counts of the number of each of the random letters created. 
 */
package countlettersinarray;

public class CountLettersInArray {

    public static void main(String[] args) {
        char[] chars = createArray();

//Display Array
        System.out.println("The lowercase are: ");
        displayArray(chars);

//count the occurrences of each letter
        int[] counts = countLetters(chars);

//Display counts
        System.out.println();
        System.out.println("The occurrences of each letter are: ");
        displayCounts(counts);
    }

    /**
     * Create and array of
     */

    public static char[] createArray() {

        //Declare an array of characters and creaate it
        char[] chars = new char[100];
        //Create lowercase letters randomly and assign them to the array
        for (int i = 0; i < chars.length; i++) {
            chars[i] = RandomCharacter.getRandomLowerCaseLetter();
        }
        //Return the array
        return chars;
    }

    /**
     * Display the array of characters
     *
     * @param chars
     */
    public static void displayArray(char[] chars) {
        for (int i = 0; i < chars.length; i++) {
            if ((i + 1) % 20 == 0) {
                System.out.println(chars[i]);
            } else {
                System.out.println(chars[i] + " ");
            }
        }
    }

    /**
     * Count the occurrences of each letter
     */
    public static int[] countLetters(char[] chars) {
        //Declare and create and array of 26 int
        int[] counts = new int[26];

        //For each lowercase letter in the array, count it
        for (int i = 0; i < chars.length; i++) {
            counts[chars[i] - 'a']++;
        }
        return counts;
    }

    /**
     * Display counts
     */
    public static void displayCounts(int[] counts) {
        for (int i = 0; i < counts.length; i++) {
            if ((i + 1) % 10 == 0) {
                System.out.println(counts[i] + " " + (char) (i + 'a'));
            } else {
                System.out.print(counts[i] + " " + (char) (i + 'a') + " ");
            }
        }
    }
}
}
}

I am desperate:( I have no idea what to do

Is there a chance you could edit your previous post and try to get the formatting to work better? It should make it easier for others to copy and paste your code :slight_smile:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums


@caleb-mabry done!

1 Like

Thank you! I am sorry, I am a senior in high school who is going to study computer science, but I am really struggling. I am an a concurrent class right now, but Arrays are hard

Now that we have a formatted version of the code, can you let us know what you’re having troubles with exactly? What exactly are you stuck on. We can work through this problem piece by piece!

2 Likes
/*
 *Write a Java program/class which creates an array with 100 random letters *using characters from ‘A’ to ‘Z’, displays the array in a 10x10 format, and displays *counts of the number of each of the random letters created. 
 */
package countlettersinarray;

public class CountLettersInArray {

    public static void main(String[] args) {
        char[] chars = createArray();

//Display Array
        System.out.println("The lowercase are: ");
        displayArray(chars);

//count the occurrences of each letter
        int[] counts = countLetters(chars);

//Display counts
        System.out.println();
        System.out.println("The occurrences of each letter are: ");
        displayCounts(counts);
    }

    /**
     * Create and array of
     */

    public static char[] createArray() {

        //Declare an array of characters and creaate it
        char[] chars = new char[100];
        //Create lowercase letters randomly and assign them to the array
        for (int i = 0; i < chars.length; i++) {
            chars[i] = RandomCharacter.getRandomLowerCaseLetter();
        }
        //Return the array
        return chars;
    }

    /**
     * Display the array of characters
     *
     * @param chars
     */
    public static void displayArray(char[] chars) {
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i] + " ");

            if ((i + 1) % 20 == 0) {
                System.out.println();
            }
        }
    }

    /**
     * Count the occurrences of each letter
     */
    public static int[] countLetters(char[] chars) {
        //Declare and create and array of 26 int
        int[] counts = new int[26];

        //For each lowercase letter in the array, count it
        for (int i = 0; i < chars.length; i++) {
            counts[chars[i] - 'a']++;
        }
        return counts;
    }

    /**
     * Display counts
     */
    public static void displayCounts(int[] counts) {
        for (int i = 0; i < counts.length; i++) {
            System.out.print((char) (i + 'a') + ":" + counts[i]);
            // ** Or use this line ** System.out.print(counts[i] + " " + (char) (i + 'a') + " ");

            if (((i + 1) % 10 == 0) || (i + 1 >= counts.length)) {
                System.out.println();
            } else {
                System.out.print(", ");
            }
        }
    }
}