Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Good morning, everyone! I am having a lot of trouble understanding this Java pro

ID: 3572640 • Letter: G

Question

Good morning, everyone! I am having a lot of trouble understanding this Java programming assignment due to absences and would greatly appreciate some help with it! The assignment is as follows:

Write a program that analyzes text written in the console by counting the number of times each of the 26 letters in the alphabet occurs. Uppercase and lowercase letters should be counted together (for example, both ‘A’ and ‘a’ should count as an A). Any characters that are not letters should be ignored. You must prompt the user to enter the text to be analyzed. Then, for any letter that appeared at least once in the text, print out the number of times it appeared (and do so in alphabetical order). An effective way to count characters is to read from the console string-by-string, and loop through all of the characters of each of these strings. Similar to Problem a, the hasNext() method of the scanner will be useful, and when testing in Eclipse, press enter and then, once on the empty line, press CTRL-D when you are done typing the text.

You must use an array to keep track of how many times each letter is seen in the text. The array should have 26 elements (one for each letter in the alphabet). Index 0 should be used to track the number of A’s in the file, index 1 to track the B’s, index 2 to track the C’s, etc., up to index 25 for the Z’s. You could use a massive if/else block, but the whole reason to use arrays is to make your programs easier. So, instead, think about how to convert each character you read into the correct index and then increment that value in the array. For example, if you read an A, then you should increment the value in index 0. Specifically, you will need to determine if the character is an uppercase letter (between ‘A’ and ‘Z’), a lowercase letter (between ‘a’ and ‘z’), or something else. If it is a letter, convert it into the appropriate index. Recall that characters and integers are interchangeable via the ASCII table conversion1. Consider this example to help get you started:

char input = 'z';

int index = input - 'a'; // index equals 25, as 'z' is 122 and 'a' is 97

You have been supplied JUnit tests for several example input texts, including an empty text, one with no letters, upper/lower/mixed case letters, and Hello World.

ALSO, the following format is provided below:

package edu.wit.cs.comp1000;

//TODO: document this class
public class PA8b {

   /**
   * Program execution point:
   * input text via console input,
   * output counts for each letter
   * found in the input (case-insensitive)
   *
   * @param args command-line arguments (ignored)
   */
   public static void main(String[] args) {
       // TODO: write your code here
   }

}

Thank you everyone so much in advance for all your help! :)

Explanation / Answer

package edu.wit.cs.comp1000;

import java.util.Scanner;

//TODO: document this class
public class PA8b {
   /**
   * Program execution point: input text via console input, output counts for
   * each letter found in the input (case-insensitive)
   *
   * @param args
   * command-line arguments (ignored)
   */
   public static void main(String[] args) {
       // TODO: write your code here
       int textTimes[] = new int[26];
       Scanner scanner = null;
       try {
           scanner = new Scanner(System.in);
           for (int i = 0; i < textTimes.length; i++) {
               textTimes[i] = 0;
           }
           System.out.print("Enter the text to analyze:");
           String text = scanner.nextLine();

           for (int i = 0; i < text.length(); i++) {
               int input = (int) text.charAt(i);

               if (input >= 65 && input <= 90) {
                   textTimes[input - 65]++;

               } else if (input >= 97 && input <= 122) {
                   textTimes[input - 97]++;
               }

           }

           System.out.println("Analyze the text:");
           for (int i = 0; i < textTimes.length; i++) {

               System.out.println((char) (65 + i) + "--" + textTimes[i]);
           }

       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
       }

   }
}

OUTPUT:

Enter the text to analyze:I am very good at java programming
Analyze the text:
A--5
B--0
C--0
D--1
E--1
F--0
G--3
H--0
I--2
J--1
K--0
L--0
M--3
N--1
O--3
P--1
Q--0
R--3
S--0
T--1
U--0
V--2
W--0
X--0
Y--1
Z--0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote