Animesh
Animesh

Reputation: 21

Why cant I return a float 2-d array from a function to main class?

This is the dbConnection.java which returns a 2-D float matrix.

   public float [][] connectToDB() throws Exception {

   Class.forName("com.mysql.jdbc.Driver").newInstance();

   // Load the database details into the variables.
   String dbUrl = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dataBase;
   dbString = dbUrl;
   try {
       // Create the database connection, if it is closed.
       if ((conn == null) || conn.isClosed()){
          conn = DriverManager.getConnection(dbString, dbUsername, dbPassword);
          Statement st = null;
          ResultSet rs = null;
          st = conn.createStatement();
          String query = "select * from Placement";
          rs = st.executeQuery(query);
          ResultSetMetaData rsmd = rs.getMetaData();
          int numCols = rsmd.getColumnCount();

          while (rs.next()) {
              float cg = rs.getFloat(1);
              float m  = rs.getFloat(2);
              float a  = rs.getFloat(3);

              if ((cg > 7.0 && cg <= 9.0) && (m > 75 && m <= 90) && (a > 75 && a <= 90)){
                  CGPA.add(cg);
                  Marks.add(m);
                  Attendance.add(a);
              }
          }

          Float[] cgpa       = CGPA.toArray(new Float[0]);
          Float[] marks      = Marks.toArray(new Float[0]); //After elgibility .... Marks are of highest weight
          Float[] attendance = Attendance.toArray(new Float[0]);

          int lengthCgpa       = cgpa.length;
          int lengthMarks      = marks.length;
          int lengthAttendance = marks.length;

          float [][] distMatrixCgpa       = new float [lengthCgpa][lengthCgpa];
          float [][] distMatrixMarks      = new float [lengthMarks][lengthMarks];
          float [][] distMatrixAttendance = new float [lengthAttendance][lengthAttendance];
          float [][] distMatrixAverage    = new float [lengthAttendance][lengthAttendance];

          for (int r = 0; r < lengthCgpa; ++r) {
              for (int c = 0; c < lengthCgpa; ++c ){
                  distMatrixCgpa [r][c] = calcDistance(cgpa[r], cgpa[c]);
              }
          }
          for (int r = 0; r < lengthMarks; ++r) {
              for (int c = 0; c < lengthMarks; ++c ){
                  distMatrixMarks [r][c] = calcDistance(marks[r], marks[c]);
              }
          }
          for (int r = 0; r < lengthAttendance; ++r) {
              for (int c = 0; c < lengthAttendance; ++c ){
                  distMatrixAttendance [r][c] = calcDistance(attendance[r], attendance[c]);
              }
          }
          for (int r = 0; r < lengthAttendance; ++r) {
              for (int c = 0; c < lengthAttendance; ++c ){
                  distMatrixAverage [r][c] = (distMatrixCgpa [r][c]
                                                + distMatrixMarks [r][c]
                                                    + distMatrixAttendance [r][c])/3;
              }
          }
          *****//This prints the correct float matrix but finally the function
          //returns a null matrix! :(
          distMatrixFormatted = new float [lengthCgpa][lengthCgpa];
          for (int r = 0; r < lengthCgpa; ++r) {
              for (int c = 0; c < lengthCgpa; ++c ) {
                  DecimalFormat df = new DecimalFormat("#.##");
                  distMatrixFormatted [r][c] = Float.valueOf(df.format(distMatrixAverage [r][c]));
                  System.out.print(distMatrixFormatted [r][c] + " ");
              }
              System.out.print("\n");
          }*****

          rs.close();
          st.close();
       }
   } catch (SQLException sqlex) {
           System.out.println("SQLException while connecting and inserting into " +
                             "the database table: " + sqlex.toString());
     } catch (Exception ex) {
           System.out.println("Exception while connecting and inserting into the" +
                              " database table: " + ex.toString());
           ex.printStackTrace();
     } finally {
           // Close the Statement and the connection objects.
           if (conn != null)
               conn.close();
     }
     return distMatrixFormatted;
}

When I call the above function in main(), it returns a null matrix.....(Agnes.java)

public static void main(String args[]) throws Exception {
            try{
                dbConnection P = new dbConnection();
                float ad[][] = P.connectToDB();
                int length = ad.length;
                double [][] Array = new double [ad.length][ad.length];

                ReadDistance readdistance = new ReadDistance();
                Array = readdistance.read(length, Array);

                *****
                //Converting Float Matrix to Double! 
                //This produces Null matrix !!!!
                for (int i = 0 ; i < Array.length ; ++i) {
                    for (int j = 0 ; j < Array.length; ++j) {
                        Array[i][j] = (double) Array[i][j];
                        System.out.print(Array[i][j] + " ");
                    }
                    System.out.print("\n");
                }*****

                Agnes agnes = new Agnes(Array, "single");
                agnes.run();
            } catch (Exception e){
                e.printStackTrace();
            }
        }

What should I do to retrieve the correct float matrix in the main function?

Please help!

PS: Both are only code snippets

Upvotes: 0

Views: 154

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502086

I believe this is the problem:

 Array[i][j] = (double) Array[i][j];

What were you expecting that to achieve? I suspect you meant:

 Array[i][j] = ad[i][j];

But in that case you wouldn't be using the previous values of Array at all... you appear to be creating two "useful" arrays of data in these two lines:

float ad[][] = P.connectToDB();
Array = readdistance.read(length, Array);

... but then you're copying one into the other. Why? Why aren't you using both?

(As an aside, I would strongly urge you to reconsider your variable naming, and also refactor connectToDb into a rather shorter method. You probably also want to call it something else as it's clearly doing more than just connecting to the database...

Upvotes: 1

Related Questions