remove objects with the same property
in
Programming Questions
•
2 years ago
hi! i have a short question:
i add new instance of an own class-object for every element in an array.
every instance gets a string-property (.wort)
like instance[3].wort="test"
what i want is: if instance[8] also get "test" as wort to remove instance 8 or at least change the poperty to "double" or whatever. i don't know how to do this. maybe it is easy to understand what i'am talking about if you have a look at the code?
the final result should count how often a words is used in the txt file.
blablabla yust have a look
it would be graet if you could give me a hint..
i add new instance of an own class-object for every element in an array.
every instance gets a string-property (.wort)
like instance[3].wort="test"
what i want is: if instance[8] also get "test" as wort to remove instance 8 or at least change the poperty to "double" or whatever. i don't know how to do this. maybe it is easy to understand what i'am talking about if you have a look at the code?
the final result should count how often a words is used in the txt file.
blablabla yust have a look
it would be graet if you could give me a hint..
- //Variablen deklarieren
PFont myFont;
String textLaden[];
String textAlles;
String textEinzeln[];
Sucher textSucher[];
int zaehler=0;
//String meinTextLaden[] = loadStrings("gedicht.txt");
//sucher klasse anlegen
class Sucher {
String wort;
int anzahl;
Sucher(String tempWort, int tempAnzahl) {
wort=tempWort;
anzahl=tempAnzahl;
}
}
//String in einzelne Wörter aufteilen und Satzzeichen etc entfernen
void setup() {
//text einladen
textLaden = loadStrings("Sido1.txt");
//text aus array in normalen string
textAlles = join(textLaden, "");
//text in die einzelnen wörter splitten und sonderzeichen etc entfernen
textEinzeln = splitTokens(textAlles, " .,!?:");
//sucher-objekte erstellen
textSucher = new Sucher[textEinzeln.length];
for (int i=0;i<textEinzeln.length;i++) {
textSucher[i]= new Sucher(textEinzeln[i].toLowerCase(), 0);
}
//vorkommen zaehlen
for (int i=0;i<textSucher.length;i++) {
for (int j=0;j<textSucher.length;j++){
if(textSucher[i].wort.equals(textSucher[j].wort)==true) {
textSucher[i].anzahl++;
}
}
//doppelte entfernen
}
/* for (int i=0;i<textSucher.length;i++) {
for (int j=0;j<textSucher.length;j++){
if((textSucher[i].wort.equals(textSucher[j].wort)==true) && (textSucher[i].wort.equals("doppel")==false)) {
textSucher[j].wort="doppel";
}
}
}*/
for (int i=0;i<textSucher.length;i++) {
println(textSucher[i].wort +" "+textSucher[i].anzahl);
}
}
1