Your program looks OK. Are you unhappy with it, and is there a specific thing in the code that you are unhappy with? Can you explain what that is?
One small change would be to use StringBuilder instead of StringBuffer - StringBuilder is a newer version of StringBuffer that doesn't have unnecessary synchronization. You will not notice any speed difference from making that change though in such a small and simple program.
In line 16, you could write counter++; instead of counter = counter + 1; but that doesn't make the code any more efficient.
When I showed it to my instructor his first impression was I was using too many variables (increasing memory footprint). I was trying to reduce the number of variables used without loosing the readability.
Thanks again for the information on StringBuilder.
Don't use toCharArray() and tempOut, use String's own length() and charAt(int) methods. That way, you don't need to copy the entire String's contents into a new char. Because that's what toCharArray() does.
Now, about the number of variables:
- input: you'll need it. That's the String to convert.
- store: you'll need it. How else can you determine when to start a new counter?
- tempOut: you can get rid of this because of my above statement.
- i: you'll need it. It's the loop counter.
- counter: you'll need it. How else can you determine how many times a character occurred?
- result: you'll need it.
So apart from tempOut you can't get rid of any of the variables.
For your information, as a little exercise for myself I rewrote your code using a nested loop. Unless I do not cache c (my version of "store") I still have the same number of variables at 5.
In this code, "c" is the equivalent of "store" and your "counter" is equivalent to "j - i". My code only has one advantage over yours - it can handle empty Strings. For the rest there isn't much difference.