Hi everyone! i am trying to find the average from an arraylist conatining database values. My database table have 9 columns and has 168 rows. Now i need to find the average at an interval of 12.

My database table has time as 1 column and 8 other columns with parameters like temperature,rainfall and so on.... Now i need to calculate the average of the values at an interval of 12 hours. i query the result and store in a resultset and then i iterated it, but i have no idea of proceeding further after that. please give some suggestion or hints so that i can able to find the result.

please show me the way. Thanks in advance.

Member Avatar for ztini

Kind of depends on how "time" is stored in your db.

But basically, you want to start a loop at time zero and continue until you hit 12 hours. Along the way you want to do 2 things. One, count the number of ResultSet rows and two, extract the temp using ResultSet.getDouble("Temp") and store each row temp as a culmulative sum. This assumes that column "Temp" is stored in your database as a double. I suspect its probably a string, so you'll need:

ResultSet rs;
String temp = rs.getString("Temp");
double tempAsDouble = Double.parseDouble(temp);

At the end of the loop, just do (sumOfTemps / countOfRows) to get your average.

Loop again if ResultSet is not empty.

Thank you for your guidance. But will you please explain me in more details. Actually, i need to calculate the average of 7 days, but with an interval of 12 hours. i have database values for 7 days. so 7*24 means 168 rows and with 9 columns and i need to have average of 12 hour interval for 1 day i.e in 1 day 2 value will come so i must have 2*7 means 14 values(avg values).

Please guide me. I am completely confused. i cannot able to proceed further. Please take me to the right track.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.