In my TheInput string, I want to replace space by '-', period by '.', new line by '!', and end-of-message by '+' before

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

In my TheInput string, I want to replace space by '-', period by '.', new line by '!', and end-of-message by '+' before

Post by answerhappygod »

In my TheInput string, I want to replace space by
'-', period by '.', new line by '!', and end-of-message by '+'
before loading it in the frequency table.
import java.util.HashMap;
import java.util.Map;
public class HuffmanFrequencyTable {
/**
* the map for recording the key and the
frequency.
*/
private Map<Character, Integer> HuffColl;
/**
* constructor of the freq table
* @param theInput the input string
*/
public HuffmanFrequencyTable(String theInput){
HuffColl = new HashMap<Character,
Integer>();
checkandadd(theInput);
}
/**
* add each character into the freq table
* @param theInput
*/
private final void checkandadd(String theInput)
{

if(theInput.length() < 2)
{
System.out.println("Length
of input is less than 2.");
}
else
{
for(int i = 0; i <
theInput.length(); i++)
{

theInput.replace(' ', '-');

theInput.replace('.', '+');
char temp =
theInput.charAt(i);

if(HuffColl.containsKey(theInput.charAt(i)))
{

HuffColl.put(temp, HuffColl.get(temp) + 1);

}

else
{

HuffColl.put(temp, 1);
}

}
}
}
/**
* return the map object recording the table.
* @return the map represents the table.
*/
public Map<Character, Integer> getTable(){
return HuffColl;
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply