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 › trouble parsing .txt...help please
Page Index Toggle Pages: 1
trouble parsing .txt...help please (Read 431 times)
trouble parsing .txt...help please
Mar 26th, 2009, 5:00am
 
Hi,

I am new to programming and processing. I am trying to parse a .txt file that contains names. The code is running but only one of the names from the "respondents" string is being displayed as text. Println() returns all of the names but text() only displays the last name in the string. I cannot figure out what I am doing wrong. Please help me and thank you.

code:

PFont font;
String data[];
String respondents;
int start;
int end;


void setup() {
 size(500,500);
 background(0, 70, 120);
 font = createFont("Serif", 24, true);
 data = loadStrings("c_p_c.txt");
 for(int i=0; i<data.length; i++) {
   start = data[i].indexOf("'") + 1;
   end = data[i].indexOf("'", start);
   respondents = data[i].substring(start, end);
   println(respondents);
 }

}

void draw() {
 textFont(font);
 text(respondents, 30, 60);
 
}
Re: trouble parsing .txt...help please
Reply #1 - Mar 26th, 2009, 6:58am
 
look closer: what is happening in your for-loop to your string variable?

think about moving the for-loop to another location in your code ...
Re: trouble parsing .txt...help please
Reply #2 - Mar 27th, 2009, 4:04pm
 
Thank you fjen. I think I may have figured it out. When the 'respondents' string is called outside of the for-loop only the last substring is recognized. I moved text() inside of the for-loop in void setup(). Is that what you were hinting at?

code:

PFont font;
String data[];
String respondents;
int start;
int end;

void setup() {
 size(1100, 600);
 background(0, 70, 120);
 font = createFont("Serif", 18, true);
 data = loadStrings("c_p_c.txt");
 for(int i = 0; i<data.length; i++) {
   start = data[i].indexOf("'") + 1;
   end = data[i].indexOf("'", start);
   respondents = data[i].substring(start, end);
   textFont(font);
   text(respondents, 65*i+12.5, 60);
 }
}

void draw() {    
}


   
 
Page Index Toggle Pages: 1