I am working on a class program and have been beating my head for several days. I need to find the number of occurrences in a string. I was able to get the results in HashMap. However, I need to be able to convert it to a single array of strings so that I can assertTrue and test it. Here is what I still have. Any suggestions would be appreciated. Thank.
public static void main(String[] args)
{
String input = "xyz 456 123 123 123 123 123 123 xy 98 98 xy xyz abc abc 456 456 456 98 xy";
String[] str = input.split(" ");
Map<String, Integer> occurrences = new HashMap<String, Integer>();
Integer oldValue = 0;
for (String value : str)
{
oldValue = occurrences.get(value);
if (oldValue == null)
{
occurrences.put(value, 1);
} else
{
occurrences.put(value, oldValue + 1);
}
}
occurrences.remove("");
}
Target array of strings:
[xy, 3, 123, 6, abc, 2, 456, 4, xyz, 2, 98, 3]
source
share