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 › stuck: failing if statement
Page Index Toggle Pages: 1
stuck: failing if statement (Read 255 times)
stuck: failing if statement
Sep 25th, 2008, 11:18pm
 
UPDATE: After browsing through the reference i finally found out that it's not possible to compare strings using the equality operator. The reference for the '==' operator states that it is possible tough...


Hi everyone. I'm terribly stuck with the sketch below. I'm trying to accomplish the following:

-Load Last.FM Chart data
-Create an ArtistPool object that gets filled with Artist objects as i parse the XML files, leaving me with a workable data structure (without doubled artists) when i'm done parsing.

I'm stuck with the getIndexInPool() function. It keeps returning -1 because the if statement in the functions fails to work

-> if (aname == testName) {

for some reason it is unable to compare the string that was passed to the function with the _artists[i].name string, yet when i compare the strings manually (by cutting and pasting the values from the xml) it works. I've tried casting values to strings, stepping thru it  manually, bassicly anything i could think of, but i'm still stuck.

Some help would be appreciated! :)




Code:

import processing.xml.*;

String chartlistFilename = "weeklychartlist.xml";
String weeklyArtistChartListFilename = "weeklyartistchart";

XMLElement xml;
ArtistPool pool;
Artist artist;

class Artist {
 
 String name;
 int[] chartPosition;
 int[] playCount;
 
 Artist(String Aname, int weeks) {
   // var setup
   name = Aname;
   chartPosition = new int[weeks];
   playCount = new int[weeks];
   //
   // prefilling the artist positions with empty values.
   //
   for (int i = 0; i<weeks; i++) {
     chartPosition[i] = 0;
     playCount[i] = 0;
   }
 }
 
 void updateArtist(int week,int chartPos,int playCnt) {
   chartPosition[week] = chartPos;
   playCount[week] = playCnt;
 }
 String artistName() {
   return name;
 }
 
}

class ArtistPool {
 
   Artist[] _artists;
 
 ArtistPool () {
   _artists = new Artist[0];
 }
 
 int getIndexInPool (String aname) {
   int index = -1;
   
   
   
   for (int i = 0; i< _artists.length;i++) {
     String testName = _artists[i].name;
     //println(aname+':'+testName);
     if (aname == testName) {
         index = i;
         println("if werkt");
       }
     
   }
 
   return index;
 }
 void updatePool (Artist artist, int artistIndex) {
   _artists[artistIndex] = artist;
   print("update");
   
 }
 
 void insertIntoPool (Artist artist) {
   _artists = (Artist[]) append(_artists, artist);
   
 }
 
 Artist getArtistFromPool(int index) {
   return _artists[index];
 }
 
 int poolSize() {
   return _artists.length;
 }
}

void setup () {

 size (200,200);
 xml = new XMLElement (this, chartlistFilename);
 int numElements = xml.getChildCount();
 pool = new ArtistPool();

String[] fileNames = new String[numElements];

 for (int i = 0; i<numElements; i++) {

   XMLElement chart = xml.getChild(i);  
   String from = chart.getStringAttribute("from");
   String to = chart.getStringAttribute("to");
   String fileName = from + "-" + to + "-" + weeklyArtistChartListFilename + ".xml";
   fileNames[i] = fileName;  
 }
 
 for (int i = 0; i<fileNames.length; i++) {
 
   xml = new XMLElement (this, fileNames[i]);
   int numElements2 = xml.getChildCount();
   for (int j = 0; j<numElements2; j++) {
 
     XMLElement artistElement = xml.getChild(j);
     XMLElement artistName = artistElement.getChild(0);
     XMLElement chartPosition = artistElement.getChild(2);
     XMLElement playCount = artistElement.getChild(3);
     
     String aName = artistName.getContent();
     int pCount = int(playCount.getContent());
     int chartPos = int(chartPosition.getContent());
     
     int currentArtistIndex = pool.getIndexInPool(aName);
     //print (currentArtistIndex);
     if (currentArtistIndex != -1) {
       
       Artist currentArtist = pool.getArtistFromPool(currentArtistIndex);
       currentArtist.updateArtist(i,chartPos,pCount);
       pool.updatePool(currentArtist,currentArtistIndex);
       
     } else {
       artist = new Artist(aName,numElements);
       artist.updateArtist(i,chartPos,pCount);
       pool.insertIntoPool(artist);
     }  
   }
 }
 String a = pool._artists[0].name;
 String b = pool._artists[5].name;
 
 println(a);
 println(b);
 
 if(a == b) {
   println("This if just won't work");
 }
}
Re: stuck: failing if statement
Reply #1 - Sep 26th, 2008, 11:23am
 
http://processing.org/reference/String.html

use the .equals method for comparing Objects in java.

aname.equals(string2)

(although this'll break if aname is null so maybe check for that first...)
Page Index Toggle Pages: 1