Counter of objects

1

I'm starting to study Java, and I was needing an object counter, that is, I need to paste a text, and in it it has to identify certain variables in that text, and from there to count the quantity of each variable, then add the variables and give the result. I do not know if it makes a difference but it would be to use the internet.

For example, I put the text in the script, and in this text it identifies elements A, B, C etc, then it adds the individually located items example:

A + A + A + A = 4A

B + B = 2B

    
asked by anonymous 27.09.2014 / 04:44

1 answer

2

Knowing the specific terms that you want to tell in a particular text, you can solve as follows:

  • Create a method that takes as arguments the text you want and the list of elements that must be counted in the text. THE idea focuses on:

    1.1 Divide the text by the spaces contained in it (using the split function); 1.2 For each element resulting from the division of the text in point 1.1, check that it it is part of the elements that are intended to be accounted for;    1.2.1 If there is, check if in HashMap there is already some element with this key    (in this case the element of the text);        1.2.1.1 If already exists, increment the value of the element on the map;        1.2.1.2 If not, insert this element into HashMap with the initial value of 1; 1.3 The% w / w will have, at the end, the amount in the text of each element.

  • Note: I recommend creating a method to check whether or not any element exists in the list of pre-determined elements to be counted.

    Example:

    //método fundamental (1.)    
        public void coontarElementosNumTexto(String texto, List<String> elementos)
            {
                HashMap<String,Integer> resMap = new HashMap();
                String[] elementosTexto = texto.split(" ");
                for (String var : elementosTexto )
                {
                    if (existeElementoLista(var,params)){
                        if (resMap .get(var) != null){
                            resMap.put(var, resMap.get(var) + 1);
                        }
                        else{
                            resMap.put(var, 1);
                        }
                    }
                }            
                System.out.println("O resultado final :\n" + resMap.toString());
            }
    

    The method I mentioned in the note might look like this:

    //método para verificar se existe um elementos na lista dos elementos pré-definidos
    private static boolean existe(String var, List<String> params) {
            for (String param : params){
                //Aqui eu ignoro se é maiúscula ou minúscula :)
                if (var.equalsIgnoreCase(param))
                    return true;
            }
            return false;
        }
    

    Usage:

    public static void main(String[] args) {
            String a = "";
            List<String> elementosContabeis = new ArrayList<>();
            elementosContabeis.add("Stack");
            elementosContabeis.add("Overflow");
            elementosContabeis.add("Pt");
    
            count("Finalmente o Stack Overflow em Pt foi lançado. Parabéns a todos que tornaram isso possível, poder ter uma versão em Pt é muito fixe!", elementosContabeis);
        }
    

    Result:

    O resultado:
    
    {Stack=1, Overflow=1, Pt=2}
    
        
    27.09.2014 / 16:20