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 › help with arrays
Page Index Toggle Pages: 1
help with arrays (Read 464 times)
help with arrays
Feb 10th, 2009, 12:12pm
 
Hey guys, I'm currently working my way through Daniel Shiffmans book as i am using processing for a project that involves using a mysql database that im pulling info from and then i want to display it. and then once i get that working start using the information i have and then manipulating it.

so far i have managed to get the info onto the sketch but it displays all on the one line on top of each other

so my ultimate question is how do i split the info up so that it will display on seperate lines
Re: help with arrays
Reply #1 - Feb 10th, 2009, 1:28pm
 
What the info looks like
Perhaps splitTokens() or split() can help you.
Re: help with arrays
Reply #2 - Feb 10th, 2009, 1:32pm
 
its going to be a text message as in sms message so not sure if split would work. i was looking at splittokens and thought if i pulled in an * from the database and use that to split the messages into separate parts.

is that the right track ?
Re: help with arrays
Reply #3 - Feb 10th, 2009, 2:01pm
 
I still don't understand where you need to split.
Perhaps you can use the width/height parameters of text() to wrap the text automatically.
Re: help with arrays
Reply #4 - Feb 10th, 2009, 2:22pm
 
this is the code im using

import de.bezier.mysql.*;
PFont f;
String [] arrayoftext = new String [200];

// created 2005-05-10 by fjenett
// updated 2005-11-16 by fjenett


MySQL msql;


void setup()
{
   size( 400, 400 );
   f = createFont("Arial",20,true);

   // this example assumes that you are running the
   // mysql server locally (on "localhost").
   //

   // replace --username--, --password-- with your mysql-account.
   //
   String user     = "daver";
   String pass     = "superhero";

   // name of the database to use
   //
   String database = "daver";

   // name of the table that will be created
   //
   String table    = "inbound";

   // connect to database of server "localhost"
   //
   msql = new MySQL( "134.36.216.17", database, user, pass, this );
   
   if ( msql.connect() )
   {  
     
       msql.query( "SELECT * FROM " + table );
       
       while (msql.next())
       {
           String s = msql.getString("message");
           int n = msql.getInt("id");
           println(s + "   " + n);
           fill(0);
           textFont(f);
           textAlign(CENTER);
           text(s,width/2,height/2);
           String arrayoftext = s;
         
           
       }
       
       // need to find out how many rows there are in table?
       //
       msql.query( "SELECT COUNT(*) FROM " + table );
       msql.next();
       println( "number of rows: " + msql.getInt(1) );
       
       
   }
   else
   {
       // connection failed !
   }
}

void draw()
{
   // i know this is not really a visual sketch ..
   
}

which gives me this in the print box

test    2
testing 123    13
this is what a text message will look like in processing   14

so i need to split up the actual text to reporduce it on screen the numbers are the row numbers which are my unique identifiers in my database but could bring in anything from my database table.

does that make more sense?

Re: help with arrays
Reply #5 - Feb 10th, 2009, 2:52pm
 
it's printing on the same line because you are printing it all on the same line 8)

offending line:
text(s,width/2,height/2);

width and height are the screen dimensions and these won't change from one line to the next.

just define a variable for the y position

text(s, width/2, y);

and increase it by the text height each time (text height = 20 in this case - see the createFont line)

also this line:

String arrayoftext = s;

doesn't do much. it's a local variable that overrides the global variable at the top there. i think you mean

arrayoftext[index] = s;

where index is incremented each time
Page Index Toggle Pages: 1