Knowing the specific terms you want to count in a given text, you can solve it as follows:
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}
If I understand correctly, you want to know the quantity of each distinct element in a given text?
– Cold
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
– Fernando Hugo
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.
– Cold
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.
– Fernando Hugo
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 ?
– Cold
That’s right :),.
– Fernando Hugo
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.
– mgibsonbr
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 ^^
– Fernando Hugo