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 › Possible datatype / variable issue
Page Index Toggle Pages: 1
Possible datatype / variable issue (Read 385 times)
Possible datatype / variable issue
Apr 9th, 2009, 9:16am
 
Hi there. I believe I'm having a simple variable issue when using processing. Can anyone take a look and see why my node availability is printing as 0.0% ? There's 100 rows in the database with a status of 'UP' for the date specified. I think its something very simple, but i'm having difficulty spotting it!! Thanks for the help! Smiley

I believe the problem lies somewhere with  rowDayPercentage

Each DB row represents a minute in my project hence the 1440 minutes in a day

Code:
import de.bezier.data.sql.*;

MySQL msql;

int nodeAvail;
int barMaxHeight = 470;
int barWidth = 80;
int hBarStart = 95;

float rowDayPercentage;

// Display percentage on graph. 470 = 100%. 4.7 = 1%
float UpTimeDisplay = rowDayPercentage * 4.7;

void setup()
{
   int hSize = 700;
   int vSize = 500;
   size(hSize, vSize);
   background(206,225,176);
   calcGraphAxisH();
   
   // MySQL specifics
   
   String user     = "root";
   String pass     = "9631";
   String date="2009-03-23";
   String database = "suasdb";
   msql = new MySQL(this, "localhost", database, user, pass);
   
   if ( msql.connect() )
   {
       msql.query( "SELECT COUNT(*) FROM alertlist WHERE status='UP' AND datetime LIKE '" + date + "%'");
       msql.next();
       
       println( "Number of rows: " + msql.getInt(1) );
       
       nodeAvail = msql.getInt(1);
       
rowDayPercentage = (nodeAvail / 1440) * 100;
       
       println( "Node Availability for day: " + rowDayPercentage + "%");
   }
   else
   {
       println("Unable to connect to Suas database!");
   }
}
       
void draw()
{
        // draw bars
     fill(126,191,11,150);
     noStroke();
     for(int j = 0; j < 560; j = j + (barWidth+5)){
     rect(hBarStart+j,barMaxHeight,barWidth,-UpTimeDisplay);
}
}

void calcGraphAxisH(){
 for(int i = 0; i <= barMaxHeight; i = i + 47){
  line(700, i, 0, i);
 }
 
}
Re: Possible datatype / variable issue
Reply #1 - Apr 9th, 2009, 9:58am
 
I believe the problem is:
Code:
rowDayPercentage = (nodeAvail / 1440) * 100;  



you're dividing an int by another int, and you're going to get 0 as the answer every time. Make it:

Code:
rowDayPercentage = (nodeAvail / 1440.0) * 100;  

Re: Possible datatype / variable issue
Reply #2 - Apr 9th, 2009, 10:02am
 
Well done John!

That worked perfectly =)

Thanks
Page Index Toggle Pages: 1