3

I need to finish a program that would take a string for example: "Mass" and would return the output somewhat like this:

M's: 1 A's: 1 S's: 2

I need to create a method that takes only one string parameter and returns an array of an object called Pair. The pair object is what i had output of above. each element in Pair contains a character and the number of occurrences of that character. This is the pair class:

public static class Pair
    {
        public char _char;
        public int _occurances;

        public Pair(char c)
        {
            _char = c;
            _occurances = 0;
        }

    }

This is the method I am working on:

public Pair[] auditString(String input) {


    return Pairs;

}

How can I make this method auditString work if I were to input "Mass" and my method returned an array of pair with this data:

pairs array would generate 3 memory locations

pairs at element 0 has character m with 1 occurrence, pairs at element 1 has character a with 1 occurrence, and pairs at element 2 has character s with 2 occurrences

Mat
  • 202,337
  • 40
  • 393
  • 406
user1504605
  • 579
  • 5
  • 19
  • 4
    if this homework you should tag it as such... – Rick Mangi Jul 05 '12 at 21:52
  • 2
    If this is homework, you should do it yourself. Or at least make an attempt on the problem and ask a specific question about where you are stuck. – chandsie Jul 05 '12 at 21:53
  • This might help http://stackoverflow.com/questions/275944/how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string – GETah Jul 05 '12 at 21:54
  • 1
    Hi, and welcome to stackoverflow. I took the liberty of adding the *homework* tag, which will help guide visitors to provide you the proper assistance in answering your questions. If this is in fact not homework, please feel free to remove it. I also removed your *subclass* and related tags as your question doesn't actually appear to address those topics. Again, you're welcome to re-edit if you think I made these changes in error. – Edward Thomson Jul 05 '12 at 21:54
  • 1
    If allowed, `Map` might be handy.. –  Jul 05 '12 at 21:54
  • 2
    Also, unlike in C/C++, Java style convention is *not* to name a private variable starting with an underscore ( _ ). – Roddy of the Frozen Peas Jul 05 '12 at 21:55

3 Answers3

1

As this is a homework question, consider the following questions:

  • How would you analyze the given input String? Clearly, you have to look at each character to compute the desired result.
  • How would you keep track of the scores? You can create a Pair object whenever you encounter a character but what do you need to do when you encounter a character for the second time? How do you get the Pair object again for that same character? Alternatively, you can also create Pair objects for each character and then find a way to compute the actual answer by doing something with Pair objects that occur multiple times (i.e., for the same character).
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
1

I'm assuming this is homework, so I'll try and avoid an answer which is too direct.

Ways in which I would suggest thinking about this problem:

  • What is a String? i.e. What is a string composed of? Strings are essentially arrays, so how would you approach this problem if they asked you to record the frequency of numbers in an array of ints? The solution to this problem is the logically the same.

  • Think about going through each 'piece' of the String one-by-one. How can you utilize the "Pair" class such that you can always keep track of the letter frequencies this way?

  • Imagine that at each letter you are visiting, you are completely oblivious to the rest of the String. What features of the Pair class (and less specifically, Objects in-general) would let you solve the problem with such little 'knowledge'?

  • Ignore the "Pair" object at first if it's confusing you at all. Try and get the logic down, first. If you can logically think out the solution in your head or on paper, the use of the Pair object should become very clear.

Hope that helps some!

AlexFZ
  • 1,439
  • 1
  • 13
  • 22
0
public Pair[] GetLetterPairs(String str)
{
    HashMap<Character, Integer> map = new HashMap<Character, Integer>();

    str = str.toLowerCase();

    for(char c : str.toCharArray())
    {
        if(!map.containsKey(c))
            map.put(c, 1);
        else
        {
            int value = map.get(c);
            map.put(c, ++value);
        }
    }

    Pair[] pairs = new Pair[map.size()];
    int i = 0;
    for(Entry<Character, Integer> entry : map.entrySet())
    {
        pairs[i] = new Pair(entry.getKey(), entry.getValue());
        i++;
    }

    return pairs;
}
MStodd
  • 4,716
  • 3
  • 30
  • 50