Object counter

Asked

Viewed 299 times

1

I’m starting to study about Java, and I was in need of an object counter, that is, I need to paste a text, and in it he has to identify certain variables in this text, and from there count the amount of each variable, then add the variables and give the result. I don’t know if it makes a difference but it would be to use in the internet.

For example, I paste the text into the script, and in that text it identifies elements A, B, C etc, then it sums the individually located items example:

A + A + A + A = 4A

B + B = 2B

  • If I understand correctly, you want to know the quantity of each distinct element in a given text?

  • That’s right, these elements are names, or quantities, for example: "John bought glasses, but the glasses, they’re defective. When the glasses were gone. Elena had glasses, even without glasses, she could see. The refrigerator was small for the place, so he tried to buy another refrigerator." 5 Glasses 2 Refrigerator

  • Do you already have a way to identify the elements to be counted? Because I realized that you intended to count each element in the text.

  • Yes, only some elements of the text will be counted, which, would be already in the programming, because when I pasted the text it would already identify.

  • So, supposing that these elements would somehow be in a list or vector, we just need to know, for each element what is its amount of text, right @Fernando Hugo ?

  • That’s right :),.

  • Please clarify the following: 1) Is it Java or Javascript? Your question was asked for Java, but you talk about "use on the internet" and "script", so it seems to me it’s about Javascript... 2) What is your specific problem? You certainly don’t want us to do all the work for you (in which case the question could be closed as "too wide"). Is your question about getting the user input? Is it about "breaking" the text into smaller components (i.e. words)? Is it about counting the words, already separated? Please [Dit] the question posting what you already have and what is causing you difficulties.

  • Since I’m starting out, there’s a lot that I’m not, I’m doing a lot of research, but, 1) that’s how, I would put the data in a "dialog box" that’s in a certain part of the site you know? 2)Specific problem is that I don’t really know much about java, and a kick start would help, rsrs, asism know where to start from 3) so it’s just "count and add" the items that from the text that was inserted, which already stretch, in a "list". As you yourself spoke separately . right was posting ^^

Show 3 more comments

1 answer

2

Knowing the specific terms you want to count in a given text, you can solve it as follows:

  1. Create a method that takes the intended text as arguments analyze and list of elements that should be counted in the text. A idea focuses on:

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

Note: I recommend the creation of a method to verify the existence or otherwise of any element in the list of predetermined elements to be accounted for.

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 may be as follows::

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

Use:

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

Upshot:

O resultado:

{Stack=1, Overflow=1, Pt=2}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.