Hi all,
I have the following code:
import java.io.;
import java.sql.;
import java.math.;
import java.lang.;
public class Testd1{
public static void main(String[] args) {
System.out.println("Sum of the specific column!");
Connection con = null;
int m=1;
double sum,sum1,sum2;
int e[];
e=new int[100];
int p;
int decimalPlaces = 5;
for( int i=0;i< e.length;i++)
{
e[i]=0;
}
double b2,c2,d2,u2,v2;
int i,j,k,x,y;
double mat[][]=new double[10][10];
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/prathi","root","mysql");
try{
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT Service_ID,SUM(consumer_feedback) FROM consumer1 group by Service_ID");
while (res.next()){
int data=res.getInt(1);
System.out.println(data);
System.out.println("\n\n");
int c1 = res.getInt(2);
e[m]=res.getInt(2);
if(e[m]<0)
e[m]=0;
m++;
System.out.print(c1);
System.out.println("\t\t");
}
sum=e[1]+e[2]+e[3]+e[4]+e[5];
System.out.println("\n \n The sum is" +sum);
for( p=21; p<=25; p++)
{
if(e[p] != 0)
e[p]=e[p]/(int)sum; //I have type casted sum to get output
BigDecimal bd1 = new BigDecimal(e[p]);
bd1 = bd1.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP); // setScale is immutable
e[p] = bd1.intValue();
System.out.println("\n\n The normalized value is" +e[p]);
mat[4][p-21]=e[p];
}
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e1){
e1.printStackTrace();
}
}
}
I have a table named consumer1.After calculating the sum i am getting the values as follows
mysql select Service_ID,sum(consumer_feedback) from consumer1 group by Service_
ID;
Service_ID sum(consumer_feedback)
31 17
32 0
33 60
34 38
35 | 38
In my program I am getting the sum for each Service_ID correctly.But,after normalization ie while I am calculating 17/153=0.111 I am getting the normalized value is 0.I want the normalized values to be displayed correctly after rounding off.My output is as follows
C:javac Testd1.java
C:java Testd1
Sum of the specific column!
31
17
32
0
33
60
34
38
35
38
The sum is153.0
The normalized value is0
The normalized value is0
The normalized value is0
The normalized value is0
The normalized value is0
But,after normalization i want to get 17/153=0.111 I am getting the normalized value is 0.I want these values to be rounded off.