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 & HelpSyntax Questions › join two strings
Page Index Toggle Pages: 1
join two strings (Read 879 times)
join two strings
Apr 28th, 2009, 12:09pm
 
Hello all, I have been working in processing for a few months now and have encountered a problem with strings. I am using simpleML to get information about rss titles and their description. For my project though, I want all of these words in a list in one array.

here is the code, the problem is under void netEvent(XMLRequest ml) also add [b]ht to front and change (dot) to . (cant post a link)

Code:

import simpleML.*;

XMLRequest xmlRequest;
String delimiters=" ,.?!;:";
String onelongstring;

void setup() {
 size(200,200);  
 // Creating and starting the request
 // An array of XML elements can be retrieved using getElementArray.
 // This only works for elements with the same name that appear multiple times in the XML document.
 xmlRequest = new XMLRequest(this, "tp://budget(dot) president(dot)ufl(dot)edu/feed/" );
 xmlRequest.makeRequest();
}
// When the request is complete
void netEvent(XMLRequest ml) {  
 // Retrieving an array of all XML elements inside"
title*
"tags
 String[] headlines = ml.getElementArray( "title");
 String[] description = ml.getElementArray( "description");
 println(headlines);
 int arlength=headlines.length;
 String onelongstring="";
 println(onelongstring);
 for (int i = 0; i < headlines.length; i++){
   //println(headlines[i]);
   //println(description[i]);
   //String[] lines= {headlines[i],description[i]};
   //println(lines);
   onelongstring = onelongstring + headlines[i]+" "+description[i]+" ";
   
   }
 }
void draw() {  
 noLoop(); // Nothing to see here
}


I am having problems forming this string with a for loop and then getting it out of the void netEvent(XMLRequest ml) funtion. Any help would be greatly appreciated. Smiley
Re: join strings, pass them to void draw
Reply #1 - Apr 28th, 2009, 12:26pm
 
Since you have defined onelongstring outside of functions, it is visible in draw().
Re: join strings, pass them to void draw
Reply #2 - Apr 28th, 2009, 1:02pm
 
thanks PhiLho

updated code, it now passes headlines to draw, I can't seem to get it to add the description lines though.

for example a headline is:To Save Money, M.I.T. Drops 8 Sports Teams
the description that goes along with it is:High school students with dreams of competing in alpine skiing at the Massachusetts Institute of Technology, beware: when this academic year ends, the institute will no longer have a varsity team.
That squad and seven others — competitive pistol, golf, wrestling, and men and women’s ice hockey and gymnastics — were eliminated on Thursday by M.I.T.’s [...]  

under the for loop, the following is not working corectly
onelongstring = onelongstring + headlines[i];
onelongstring = onelongstring + description[i];

Code:

import simpleML.*;

XMLRequest xmlRequest;
String delimiters=" ,.?!;:";
String onelongstring;

void setup() {
 size(200,200);  
 // Creating and starting the request
 // An array of XML elements can be retrieved using getElementArray.
 // This only works for elements with the same name that appear multiple times in the XML document.
 xmlRequest = new XMLRequest(this, "http://budget.president.ufl.edu/feed/" );
 xmlRequest.makeRequest();
}
// When the request is complete
void netEvent(XMLRequest ml) {  
 // Retrieving an array of all XML elements inside"
title*
"tags
 String[] headlines = ml.getElementArray( "title");
 String[] description = ml.getElementArray( "description");
 println(headlines);
 int arlength=headlines.length;
 String onelongstring="";
 //println(onelongstring);
 for (int i = 0; i < headlines.length; i++){    
   onelongstring = onelongstring + headlines[i];
   onelongstring = onelongstring + description[i];
   
   }
 }
void draw() {
 println(onelongstring);
 noLoop(); // Nothing to see here
}
Re: join two strings
Reply #3 - Apr 28th, 2009, 1:38pm
 
Makes two separate loops. And use onelongstring += description[i]; (identical but easier to read and type!).
Re: join two strings
Reply #4 - Apr 28th, 2009, 2:03pm
 
thanks again PhiLho, your incredibly helpful. i think i just got it to work though.

here is what the bottom code looks like, i just had to add the string together correctly and take out other code.

Code:
void netEvent(XMLRequest ml) {  

 String[] headlines = ml.getElementArray( "title");
 String[] description = ml.getElementArray( "description");
 for (int i = 0; i < headlines.length; i++){  
//most important line    
  onelongstring = onelongstring+headlines[i]+" "+description[i]+" ";    
   }
 println(onelongstring);
}
 
void draw() {
 //println(onelongstring);
 noLoop(); // Nothing to see here
}

however the println in void draw still won't work(if its uncommented), I think I now have to work with the Asychronous Requests that are a part of simpleML(I actually want my sketch to wait until all of the data is loaded). I think i can find it on another post though

nevermind i am retarded, I just have to eliminate noloop and I don't need to println every run through draw. Thanks
Page Index Toggle Pages: 1