简体   繁体   中英

Java: Method that takes a string and returns the characters along with occurences of each character

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

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 (ie, for the same character).

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? ie 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 int s? 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!

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM