4

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]
2
  • How do u want to convert the hashmap into string. It is not clear. Commented Aug 13, 2016 at 13:59
  • Is this question fully answered? Then please accept it as an answer or post a follow up question. Thanks. Commented Aug 14, 2016 at 12:15

1 Answer 1

3

Is the question, how you can read the key-value-pairs from your hashmap?

Then the following example demonstrates some simple readings:

for(String entry : occurrences.keySet()) {
    Integer value = occurrences.get(entry);         
    System.out.println(entry + ":" + value);
}

Output:

xy:3
123:6
abc:2
456:4
xyz:2
98:3

Update:

to get a string array [key, value, key, value, ...] use the following code:

ArrayList<String> strings = new ArrayList<>();

for(String entry : occurrences.keySet()) {
    strings.add(entry);
    strings.add(""+occurrences.get(entry));
}

String[] asArray = strings.toArray(new String[strings.size()]);

Or without ArrayList:

String[] asArray = new String[occurrences.size()*2];
int index = 0;

for(String entry : occurrences.keySet()) {
    asArray[index++]=entry;
    asArray[index++]=""+occurrences.get(entry);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Not exactly. I need to test a string that results in an array that calculates the number of occurrences a word appears in a string. I was able to get the expected result in a hash map but now I need to convert it to a string array so that I can test it against the actual result.
Can you give us "the actual result", so we can help you? Otherwise we have to guess, what format the strings in the string[] should have. Since you have a string (the keys) and the Integers (the values), you have to combine them in some format if you intend to store them in a single array of strings.
Oh sure. Sorry about that. In this case the result should be:
In the form of an array: [xy, 3, 123, 6, abc, 2, 456, 4, xyz, 2, 98, 3]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.