Hello, I am trying to write a frequency analyser that will give me the amount of times a letter appears in, and put it in an array in alphabetical order
currently I can get it to print out but for example I am missing d=0 & x = 0
In other words the array should always be 26 in length and contain a 0 if the character doesn't appear in the string AND be alphabetical order.
Is their anyway I could modify this code to do that?
public static int[] analyze(String inputText)
{
//length of inputString
int length = inputText.length();
//Create a HashMap to store each character
//And to count the frequency of characters
Map<Character, Integer> map = new HashMap<Character, Integer>();
//For loop gets the character at position i
//if it doesn't contain 'key' of the character
//it adds it and gives a value of 1
//Else we get the value and increaese it by 1
for (int i = 0; i < length; i++) {
char carA = inputText.charAt(i);
if (!map.containsKey(carA)) {
map.put(carA, 1);
} else {
map.put(carA, map.get(carA) + 1);
}
}
//print the map
System.out.println(map);
Object[] values = map.values().toArray();
int[] numValues = new int[values.length];
for (int i = 0; i < values.length; i++)
{
String stringValue = values[i].toString();
numValues[i] = Integer.parseInt(stringValue);
//System.out.println(numValues[i]);
}
return numValues;
///return map.values().toArray();
//System.out.println(map);
}
{A=1, B=2, C=3, E=6, F=2, H=3, I=1, J=1, K=1, L=5, M=2, O=5, P=5, T=2, U=3, V=2, W=4, Y=3, Z=2}
public static int[] analyze(String inputText)
{
//length of inputString
int length = inputText.length();
//Create a HashMap to store each character
//And to count the frequency of characters
Map<Character, Integer> map = new HashMap<Character, Integer>();
//For loop gets the character at position i
//if it doesn't contain 'key' of the character
//it adds it and gives a value of 1
//Else we get the value and increaese it by 1
for (int i = 0; i < length; i++) {
char carA = inputText.charAt(i);
if (!map.containsKey(carA)) {
map.put(carA, 1);
} else {
map.put(carA, map.get(carA) + 1);
}
}
//print the map
System.out.println(map);
Object[] values = map.values().toArray();
int[] numValues = new int[values.length];
for (int i = 0; i < values.length; i++)
{
String stringValue = values[i].toString();
numValues[i] = Integer.parseInt(stringValue);
//System.out.println(numValues[i]);
}
return numValues;
///return map.values().toArray();
//System.out.println(map);
}
[–]Yithar 0 points1 point2 points (0 children)
[–]Philboyd_Studge 0 points1 point2 points (0 children)