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

hi need some help with a program, i have got this to setup like this with some h

ID: 3734922 • Letter: H

Question

hi need some help with a program, i have got this to setup like this with some help, but how would i change it to the modification listed below, aka best way to do it

Modify the program of Fig. 16.18 to count the number of occurrences of each letter rather than of each word. For example, the string "HELLO THERE" contains two Hs, three Es, two Ls, one O, one T and one R. Display the results.

Run your program with the following string: Hi there, I am taking Java Programming (Intermediate).

import java.util.ArrayList;

import java.util.Collections;

import java.util.LinkedList;

import java.util.List;

public class Generic_Collection {

   public static void main(String a[]) {

   LinkedList<Character> list = new LinkedList<Character>();

   list.add('F');

   list.add('G');

   list.add('U');

   list.add('V');

   list.add('L');

   list.add('A');

   list.add('I');

   list.add('D');

   list.add('R');

   list.add('P');

   System.out.println("Actual LinkedList:" + list);

   List<Character> listCopied = new ArrayList<Character>();

   listCopied.addAll(list);

   //Reverse the listCopied.

   Collections.reverse(listCopied);

   System.out.println("List: " + list);

   System.out.println("Copy List: " + listCopied);

  

   }

   }

Explanation / Answer

CharCountTest.java

import java.util.HashMap;

import java.util.Scanner;

import java.util.Set;

import java.util.TreeMap;

import java.util.Map.Entry;

public class CharCountTest {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter the string: ");

String s = scan.nextLine();

HashMap<Character, Integer> map = new HashMap<Character, Integer>();

for(int i=0;i<s.length();i++) {

if(s.charAt(i) != ' ') {

if(map.get(s.charAt(i)) != null) {

map.put(s.charAt(i), map.get(s.charAt(i))+1);

} else {

map.put(s.charAt(i), 1);

}

}

}

TreeMap<Character, Integer> sorted = new TreeMap<Character, Integer>(map);

Set<Entry<Character, Integer>> mappings = sorted.entrySet();

System.out.println("HashMap after sorting by keys in ascending order ");

for(Entry<Character, Integer> mapping : mappings){

System.out.println(mapping.getKey() + " ==> " + mapping.getValue()); }

}

}

Output:

Enter the string:
HELLO THERE
HashMap after sorting by keys in ascending order
E ==> 3
H ==> 2
L ==> 2
O ==> 1
R ==> 1
T ==> 1