tags:

views:

46

answers:

2

Hi all,

I have the following code:

import java.sql.*;
import java.math.*;

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.

+1  A: 
int e[];

...

e[p] = bd1.intValue();

e is an array of integers. How do you expect the value of an integer to 0.111?

Change e to some type that can hold decimal fractions.

tom
+1  A: 

Some other comments:

I think you want to set the following to 0, since your e array seems to be zero based.

int m = 1;

Local variables in Java don't get initialized to any default value, so it's good practice to initialize variables when you define them:

double sum = 0.0, sum1 = 0.0, sum2 = 0.0;

Realize that you've hard-coded the size of your e array, but your query could return a greater number of rows, and your program would crash.

int e[] = new int[100];

The following code:

e[p] = e[p] / (int) sum;

...won't do what you want. Here's another view of what you're doing:

int = int / (int) double;

You're storing the result in an integer variable, so you've already lost the fractional portion of your result before you even get to the following line:

BigDecimal bd1 = new BigDecimal(e[p]);
Scott Smith