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.
Page Index Toggle Pages: 1
String Arrays (Read 189 times)
String Arrays
Feb 27th, 2009, 9:08pm
 
hello,
i am trying to pass multiple links thru my draw function, but i'm not sure if i am using string arrays correctly? does this make sense? thanks.

import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.*;

int a=2;

String[] feedurl={"http://sfbay.craigslist.org/search/mis/sfc?query=you&minAsk=min&maxAsk=max&neighborhood=&format=rss&s=0","http://sfbay.craigslist.org/search/mis/sfc?query=you&minAsk=min&maxAsk=max&neighborhood=&format=rss&s=25",
                 };

 
FeedReader[] feed=new FeedReader[a];

void setup() {
 size(800, 800);
 smooth();
 noLoop();
}

void draw(){
 background(255);
 
 feed=new FeedReader[feedurl];

// print feed data
 println("Loading feed: "+ feedurl);
 println("Feed: "+feed[a].title);
 println("------------------------------");
 println("Description: "+feed[a].description);
 println("\nNumber of entries: "+feed[a].numEntries);
 println("------------------------------");
   
 // print feed entries
 for(int i=0; i < feed[a].numEntries; i++) {
   noStroke();
     
   if(feed[a].entry[i].foundRed == true){
      fill(255, 0, 0);
      ellipse(5+i*8, 10, 5, 5);
   } else{
      fill(0);
      ellipse(5+i*8, 10, 1, 1);
   }  
   if(feed[a].entry[i].foundOrange == true){
      fill(#F27318);
      ellipse(5+i*8, 20, 5, 5);
   }
   
 }
 
 
 class FeedReader {
 SyndFeed feed;
 String url,description,title;
 int numEntries;
 FeedEntry entry[];
 public FeedReader(String _url) {
   url=_url;
   try {
     feed=new SyndFeedInput().build(new XmlReader(new URL(url)));
     description=feed.getDescription();
     title=feed.getTitle();

     java.util.List entrl=feed.getEntries();
     Object [] o=entrl.toArray();
     numEntries=o.length;

     entry=new FeedEntry[numEntries];
     for(int i=0; i < numEntries; i++) {
       entry[i]=new FeedEntry((SyndEntryImpl)o[i]);
       println(i+": "+entry[i]);
     }
   }
   catch(Exception e) {
     println("Exception in Feedreader: "+e.toString());
     e.printStackTrace();
   }
 }

}
 
 
Re: String Arrays
Reply #1 - Feb 28th, 2009, 8:44am
 
I am not sure what is your problem, but I see a number of issues with your code:
rrrr wrote on Feb 27th, 2009, 9:08pm:
[1] int a=2;
String[] feedurl={};
FeedReader[] feed=new FeedReader[a];

[2]   feed=new FeedReader[feedurl];

[3]   println("Feed: "+feed[a].title);
   
[4]       java.util.List entrl=feed.getEntries();
     Object [] o=entrl.toArray();
     numEntries=o.length;

[1] Minor issue here, but you can use feedurl.length to initialize the size of feed.
BTW, there is a quite well established convention to put a plural to array names: feedURLs, feeds, etc.

[2] This line is confusing. Syntax is wrong (need an integer, perhaps .length as shown). And you are overwritting the initial assignment.
The new T[n] syntax is to create arrays of type T with n entries. It returns T[] type. The new T(p1, p2) syntax is to create an object of type T by giving parameters p1, p2 to class T's constructor.

[3] a is 2, length of feed is 2, ie. with indexes 0 and 1: you will get an ArrayOutOfBounds exception.

[4] You don't need a toArray to get the size of a List! numEntries = entrl.size(); should do the work.

HTH.
Page Index Toggle Pages: 1