I'm working on a program for class and have been beating my head for days on this. I have to find the number of occurrences in a string. I've been able to get the results into a HashMap. However, I need to be able to convert it to a single string array so that I can assertTrue and test it. Here's what I have so far. Any suggestions would be greatly appreciated. Thanks.
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 to be tested
String[] str = input.split(" "); // String put into an array
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("");
}
The target string array:
[xy, 3, 123, 6, abc, 2, 456, 4, xyz, 2, 98, 3]