Home > database >  How do I edit this coding to show the characters with frequency 0. eg: a=0. Because now it only show
How do I edit this coding to show the characters with frequency 0. eg: a=0. Because now it only show

Time:01-10

Write a program that will read a line of text String and display all the letters that occur in the text, one per line and in alphabetical order, along with the number of times each letter occurs in the text.

For this purpose, you must use an array of type int of length 26, so that the element at index 0 contains the number of a’s, the element at index 1 contains the number of b’s, and so forth.

Allow both uppercase and lowercase letters as input, but treat uppercase and lowercase versions of the same letter as being equal.

Hint: Use the method chatAt(int index) in the String class to get the individual character in a string at the specified index.

public class Test {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] letters = new int[26];
        char choice;

        while (true) {
            // taking user input
            System.out.println("Please enter text ending with period:");
            String text = sc.nextLine();

            // converting it lowercase
            text = getActualText(text).toLowerCase();

            char c = 'a';

            for (int i = 0; i < letters.length; i  )
                // increasing character by 1
                letters[i] = countLetters(text, c  );

            System.out.println("\nThe frequency of the letters");
            c = 'a';

            for (int i = 0; i < letters.length; i  ) {
                // showing only those letters whose frequnecy is greater than 0
                if (letters[i] != 0)
                    System.out.println(c   ": "   letters[i]);

                c  ;
            }

            System.out.print("Would you like to try another text?(Y/N) ");
            choice = sc.nextLine().charAt(0);

            if (choice == 'n' || choice == 'N')
                break;
        }
    }

    private static int countLetters(String text, char c) {
        int count = 0;

        for (int i = 0; i < text.length(); i  )
            // counting the frequency
            if (text.charAt(i) == c)
                count  ;

        return count;
    }

    /**
     * This method will extract the first sentence from a text ending with full stop(.)
     */
    private static String getActualText(String text) {
        String newText = "";

        for (int i = 0; i < text.length(); i  ) {
            if (text.charAt(i) == '.')
                // breaking out of the loop if the full stop is found
                break;

            // adding it to the text
            newText  = text.charAt(i)   "";
        }

        return newText;
    }

}

CodePudding user response:

Try to change existing condition to below new condition:

Existing Condition: (Allowing frequencies which are not equal to 0):

if(letters[i] != 0) {//showing only those letters whose frequency is greater than 0

New Condition: (Allowing frequencies which are greater than or equal to 0):

if(letters[i] >= 0) {

CodePudding user response:

It's enough to go through the text one time and count the occurrence of each letter. And then just show only letters with count >0.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    do {
        System.out.print("\nEnter the text: ");
        String str = scan.nextLine();
        print(histogram(str));
    } while (shouldContinue(scan));
}

private static int[] histogram(String str) {
    int[] letters = new int[26];

    for (int i = 0; i < str.length(); i  )
        if (Character.isLetter(str.charAt(i)))
            letters[Character.toLowerCase(str.charAt(i)) - 'a']  ;

    return letters;
}

private static void print(int[] letters) {
    System.out.println("The frequency of the letters:");

    for (int i = 0; i < letters.length; i  )
        if (letters[i] > 0)
            System.out.println((char)('a'   i)   ": "   letters[i]);
}

private static boolean shouldContinue(Scanner scan) {
    while (true) {
        System.out.print("Would you like to try another text (Y/N)? ");
        String str = scan.nextLine();

        if (str.length() != 1)
            continue;
        if ("Y".equalsIgnoreCase(str))
            return true;
        if ("N".equalsIgnoreCase(str))
            return false;
    }
}
  •  Tags:  
  • Related