We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Array loop problem- please help!
Page Index Toggle Pages: 1
Array loop problem- please help! (Read 264 times)
Array loop problem- please help!
Nov 18th, 2008, 3:40pm
 
Hi everyone,
I am trying to build something based on Daniel Shiffman's idea for the HashMap example - http://processing.org/learning/topics/hashmapclass.html - and I am stuck at my first step.I am tryig to replace the HashMap with an array of objects so I can keep the order of each word in the text.

I am trying to iterate through the array and increase the size of the words that are more common.
I've got a function that I call from draw...

void draw() {

 background(255);
 fill(0);
 noStroke();
 textFont(f, 5);
 
 for (int i =0; i < wordArray.length; i++){
   
   wordArray[i].update();
   
 }

 makeWords(counter);
 
 counter++;
 
}

void makeWords(int count){

 String s = wordArray[count].word;

 for(int i = 0; i < wordArray.length; i++){

      if (wordArray[i].word == s ) {
   
      wordArray[i].count();

  }   else {
     wordArray[count].start(x, y);
 }
}
}

the update() method of the Word object displays the text and
the count() has a countWord++; line in it which would increase the occurrence hence the size of each word.

My problem is that inside the for loop the if statement gets executed only once.
If later in the text (in the wordArray )there's an instance of the same word and the if statement should return true, it ignores it and the code in the if statement doesn't run.
I checked it by tracing something after the for statement and after the if statement as well. The latter appeared only once.
I've been trying to find the solution for more than a day now, I tried to use nested loops, I tried using ArrayLists with Iterators - no luck.

I would appreciate very much any comments or advises!
Thank you!
b
Re: Array loop problem- please help!
Reply #1 - Nov 18th, 2008, 4:10pm
 
you can't use == with Strings (or any other Object), have to use .equals()

http://processing.org/reference/String.html
Re: Array loop problem- please help!
Reply #2 - Nov 18th, 2008, 4:23pm
 
oh, ok.Smiley
I would've never thought of that.
Thank you very much!
Page Index Toggle Pages: 1