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

JAVA please help Exercise 4: Write a Java program that reads a string from the u

ID: 641982 • Letter: J

Question

JAVA please help Exercise 4: Write a Java program that reads a string from the user, and then prints out the count of each letter (regardless case) as shown below. Note that you need to use separate counter for each letter, that is 26 counters, named as d Count. b Count. c Count. d Count, Print out only the counters that count the letters used in the string (that is, counters with values > 0 only) Do not printer counters with 0 values Use proper labels for the outputs as shown in the examples below.

Explanation / Answer

import java.util.Scanner;

public class CountAlphabets {
   public static void main(String args[]) {
       Scanner in = new Scanner(System.in);
       int acount = 0, bcount = 0, ccount = 0, dcount = 0, ecount = 0
               , fcount = 0, gcount = 0, hcount = 0, icount = 0, jcount = 0
               , kcount = 0, lcount = 0, mcount = 0, ncount = 0, ocount = 0
               , pcount = 0, qcount = 0, rcount = 0, scount = 0, tcount = 0
               , ucount = 0, vcount = 0, wcount = 0, xcount = 0, ycount = 0, zcount = 0;
       System.out.print("Entered string: ");
       String text = in.nextLine();
       for(int i = 0; i < text.length(); ++i){
           if(text.charAt(i) == 'a' || text.charAt(i) == 'A'){
               acount++;
           }
           else if(text.charAt(i) == 'b' || text.charAt(i) == 'B'){
               bcount++;
           }
           else if(text.charAt(i) == 'c' || text.charAt(i) == 'C'){
               ccount++;
           }
           else if(text.charAt(i) == 'd' || text.charAt(i) == 'D'){
               dcount++;
           }
           else if(text.charAt(i) == 'e' || text.charAt(i) == 'E'){
               ecount++;
           }
           else if(text.charAt(i) == 'f' || text.charAt(i) == 'F'){
               fcount++;
           }
           else if(text.charAt(i) == 'g' || text.charAt(i) == 'G'){
               gcount++;
           }
           else if(text.charAt(i) == 'h' || text.charAt(i) == 'H'){
               hcount++;
           }
           else if(text.charAt(i) == 'i' || text.charAt(i) == 'I'){
               icount++;
           }
           else if(text.charAt(i) == 'j' || text.charAt(i) == 'J'){
               jcount++;
           }
           else if(text.charAt(i) == 'k' || text.charAt(i) == 'K'){
               kcount++;
           }
           else if(text.charAt(i) == 'l' || text.charAt(i) == 'L'){
               lcount++;
           }
           else if(text.charAt(i) == 'm' || text.charAt(i) == 'M'){
               mcount++;
           }
           else if(text.charAt(i) == 'n' || text.charAt(i) == 'N'){
               ncount++;
           }
           else if(text.charAt(i) == 'o' || text.charAt(i) == 'O'){
               ocount++;
           }
           else if(text.charAt(i) == 'p' || text.charAt(i) == 'P'){
               pcount++;
           }
           else if(text.charAt(i) == 'q' || text.charAt(i) == 'Q'){
               qcount++;
           }
           else if(text.charAt(i) == 'r' || text.charAt(i) == 'R'){
               rcount++;
           }
           else if(text.charAt(i) == 's' || text.charAt(i) == 'S'){
               scount++;
           }
           else if(text.charAt(i) == 't' || text.charAt(i) == 'T'){
               tcount++;
           }
           else if(text.charAt(i) == 'u' || text.charAt(i) == 'U'){
               ucount++;
           }
           else if(text.charAt(i) == 'v' || text.charAt(i) == 'V'){
               vcount++;
           }
           else if(text.charAt(i) == 'w' || text.charAt(i) == 'W'){
               wcount++;
           }
           else if(text.charAt(i) == 'x' || text.charAt(i) == 'X'){
               xcount++;
           }
           else if(text.charAt(i) == 'y' || text.charAt(i) == 'Y'){
               ycount++;
           }
           else if(text.charAt(i) == 'z' || text.charAt(i) == 'Z'){
               zcount++;
           }
       }
       if(acount != 0){
           System.out.println("Letter A: " + acount);
       }
       if(bcount != 0){
           System.out.println("Letter B: " + bcount);
       }
       if(ccount != 0){
           System.out.println("Letter C: " + ccount);
       }
       if(dcount != 0){
           System.out.println("Letter D: " + dcount);
       }
       if(ecount != 0){
           System.out.println("Letter E: " + ecount);
       }
       if(fcount != 0){
           System.out.println("Letter F: " + fcount);
       }
       if(gcount != 0){
           System.out.println("Letter G: " + gcount);
       }
       if(hcount != 0){
           System.out.println("Letter H: " + hcount);
       }
       if(icount != 0){
           System.out.println("Letter I: " + icount);
       }
       if(jcount != 0){
           System.out.println("Letter J: " + jcount);
       }
       if(kcount != 0){
           System.out.println("Letter K: " + kcount);
       }
       if(lcount != 0){
           System.out.println("Letter L: " + lcount);
       }
       if(mcount != 0){
           System.out.println("Letter M: " + mcount);
       }
       if(ncount != 0){
           System.out.println("Letter N: " + ncount);
       }
       if(ocount != 0){
           System.out.println("Letter O: " + ocount);
       }
       if(pcount != 0){
           System.out.println("Letter P: " + pcount);
       }
       if(qcount != 0){
           System.out.println("Letter Q: " + qcount);
       }
       if(rcount != 0){
           System.out.println("Letter R: " + rcount);
       }
       if(scount != 0){
           System.out.println("Letter S: " + scount);
       }
       if(tcount != 0){
           System.out.println("Letter T: " + tcount);
       }
       if(ucount != 0){
           System.out.println("Letter U: " + ucount);
       }
       if(vcount != 0){
           System.out.println("Letter V: " + vcount);
       }
       if(wcount != 0){
           System.out.println("Letter W: " + wcount);
       }
       if(xcount != 0){
           System.out.println("Letter X: " + xcount);
       }
       if(ycount != 0){
           System.out.println("Letter Y: " + ycount);
       }
       if(zcount != 0){
           System.out.println("Letter Z: " + zcount);
       }
   }
}