调用2个类java时出错

时间:2012-04-06 07:25:35

标签: java oop

我需要从一个类中调用两个类,基本上我需要调用的那两个类的内容是相同的,只是在查询中有所不同,但是在创建对象和调用第二个类时我遇到了错误。这是我要调用的代码

 Tree tree = new Tree();
        return tree.hashtree(dbtoarray.getdata(), dbtoarray.GetDataPrediction());

        TreeApplicationSubject treeA = new TreeApplicationSubject();//error sign:unreachable statement
        return treeA.hashtree(dbtoarray.getdata(), dbtoarray.GetDataPrediction());

以下是我需要在第二个来电者中调用的课程

package Engine;
import java.util.*;
/**
 *
 * @author fobia
 */
public class TreeApplicationSubject {

    double[] correlation;
    double[] meanArr;

    public double[] getCorrelation() {
        return correlation;
    }

    public double[] getMeanArr() {
        return meanArr;
    }

    public String[] hashtree(String[][] dataset, String[][] predictiondata) {
        //======fetch data from database then add 1 morre row for calculation

        String temp = "";

        String[] Means = {"Means", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};

        //================= combine 3 array =====================================
        System.out.println("dataset.length: " + dataset.length + "\npredictiondata.length" + predictiondata.length);
        int heightofarray = (dataset.length) + (predictiondata.length) + 1;
        int lengthofarray = (dataset[0].length);
        System.out.println("heightofarray: " + heightofarray + " lengthofarray: " + lengthofarray);

        //TextArea
        temp = temp + "\n" + "heightofarray: " + heightofarray + " lengthofarray: " + lengthofarray;
        String[][] toCalculate = new String[heightofarray][lengthofarray];

        for (int a = 0; a < dataset.length; a++) {
            for (int b = 0; b < dataset[0].length; b++) {
                toCalculate[a][b] = dataset[a][b];
            }
        }
        for (int a = 0; a < predictiondata.length; a++) {
            for (int b = 0; b < predictiondata[0].length; b++) {
                toCalculate[(dataset.length) + a][b] = predictiondata[a][b];
            }
        }
        for (int a = 0; a < Means.length; a++) {
            toCalculate[heightofarray - 1][a] = Means[a];
        }

        //==========================mulai kalkulasi=============================
        System.out.println("harusnya muncul String[][]toCalculate");
        //TextArea
        temp = temp + "\n" + ("harusnya muncul String[][]toCalculate");

        for (int a = 0; a < toCalculate.length; a++) {
            for (int b = 0; b < toCalculate[0].length; b++) {
                System.out.print(toCalculate[a][b] + "\t");
            }
            System.out.println("");
        }


        System.out.println("=============================== hashtree ====================================");
        System.out.println("                       value in root position\n");

        //ngeprint dataset per row serta memecah nilai means, kalau di tree ini adalah root. dlm method ini smua data dimasukkan ke root
        for (int y = 1; y < toCalculate[0].length; y++) {
            System.out.println("dataset " + y + " is ");
            //TextArea
            temp = temp + "\n" + ("dataset " + y + " is ");
            //---
            double manto = 0;
            for (int x = 0; x < toCalculate.length - 1; x++) {//biar mulai itung data dr sini
                manto = manto + Double.parseDouble(toCalculate[x][y]);
                //nilai array per row
                System.out.print(toCalculate[x][y]);


            }
            System.out.println("\tmeans value: " + (manto / (toCalculate.length - 1)));
            toCalculate[toCalculate.length - 1][y] = "" + (manto / (toCalculate.length - 1));

        }


        System.out.println("lalallalallaa~");
        Print.Print(toCalculate);
        System.out.println("lalallalala");
        AvgDev avgDev = new AvgDev();
        //    String[][] newCombine = avgDev.FindAvg(toCalculate);//mengcombine array blm dipecah

      //        temp = temp + "\n" + ("harusnya masih nol 00");



        //mulai mengisi
        Map mapTree = new HashMap();

        //   int numClusters = 2;// int akar=2--> number of cluster;
        int treeDeep = 3;//int maxNode = 2;
        int jumlah = 1;//counter

        //positioning for childs
        for (int x = 0; x <= treeDeep; x++) {
            for (int y = 1; y <= jumlah; y++) {
                //initialize position. position adalah nomor identitas setiap node
                String position = x + "." + y;//x itu deep, y itu nomor children
                mapTree.put(position, null);
            }
            jumlah = jumlah * 2;//karena binary tree
        }

        //String sementara untuk membangun tree
        //all ID are turned into String (not string array) to enter the tree
        String root = "";
        for (int i = 1; i < toCalculate.length; i++) {
            if (i == 1) {
                root = toCalculate[i][0];
            } else {
                root = root + "," + toCalculate[i][0];
            }
        }
        mapTree.put("0.1", root);//0.1 root tree identity, since it's root, all comes up here
        System.out.println("=== Printing map tree ===");
        System.out.println("mapTree: "+mapTree);
        System.out.println("root vale: "+root);
        jumlah = 1;
        String[][] data = new String[0][0];//the strings value name is now become data
        for (int i = 0; i < treeDeep; i++) {
            for (int j = 1; j <= jumlah; j++) {
                String IDRoot = (i + "." + j);
                if (mapTree.get(IDRoot) != null) {
                    if (!mapTree.get(IDRoot).toString().trim().equals("")) {
                        String[] getMap = mapTree.get(IDRoot).toString().split(",");
                        data = new String[getMap.length][toCalculate[0].length];
//                        for (int k = 0; k < toCalculate[0].length; k++) {
//                            data[0][k] = toCalculate[0][k];
//                        }
                        for (int k = 0; k < getMap.length; k++) {
                            for (int l = 0; l < toCalculate.length; l++) {

                                //  System.out.println("aw>" + getMap[k]+"><"+toCalculate[l][0]);
                                if (getMap[k].equals(toCalculate[l][0])) {

                                    //   System.out.println("masuk");
                                    data[k][0] = getMap[k];
                                    for (int m = 1; m < toCalculate[0].length; m++) {
                                        data[k][m] = toCalculate[l][m];
                                    }
                                }
                            }
                        }

                        System.out.println("data.length>" + data.length);
                        System.out.println(">>" + "" + (i) + "." + (j));
                        //testing the tree (masuk apa gak)
                        for (int p = 0; p < data.length; p++) {
                            for (int q = 0; q < data[0].length; q++) {
                                System.out.print(data[p][q] + "|");
                            }

                            System.out.println();
                        }
                        System.out.println("WUZZZ");
                        //-----

                        KMeansClustering kmeans = new KMeansClustering();//call KMeans

                        if (data.length > 1) {


                            List valueclust = kmeans.Kmeanz(data);
                            //checking for the cluster value (checking only)
                            for (int p = 0; p < valueclust.size(); p++) {
                                String[] wew = valueclust.get(p).toString().split(";");
                                for (int q = 0; q < wew.length; q++) {
                                    System.out.print(wew[q] + "|");
                                }
                                System.out.println();
                            }
                            //----

                            //inserting to the tree
                            int numb = 1;//counter for cluster numbering (1 or 2)
                            for (int p = 0; p < valueclust.size(); p++) {
                                System.out.println("=================================================================");
                                System.out.println("CLUSTER" + numb);
                                String clusterElement = "";//strings containing the cluster elements of a clusters
                                clusterElement = "" + valueclust.get(p);
                                String judul = "";//child positioning
                                if (numb == 1) {
                                    judul = "" + (i + 1) + "." + (j * 2 - 1);
                                } else if (numb == 2) {
                                    judul = "" + (i + 1) + "." + (j * 2);
                                }
                                System.out.println("Cluster elements: " + judul + ":" + clusterElement);
                                mapTree.put(judul, clusterElement);
                                numb++;
                            }
                        }
                    }
                }
            }
            jumlah = jumlah * 2;
        }
        data = null;

        /////??????????????????????????????????????????????????????????????????????????

        // tambahan baru nih

        /////??????????????????????????????????????????????????????????????????????????

        //untuk menginisialisasi anggota cluster yang didalamnya terdapat dataset Means value
        String adaMeans = "";
        for (int i = 0; i < mapTree.size(); i++) {
            String mapStr = (String) mapTree.get(treeDeep + "." + i);

            if (mapStr != null) {
                String[] mapStrArr = mapStr.split(",");
                for (int j = 0; j < mapStrArr.length; j++) {
                    if (mapStrArr[j].equals("Means")) {
                        adaMeans = mapStr;
                    }
                }
            }
        }
        //dibawah ini untuk display anggota cluster terpilih (yg ada data means)
        System.out.println("Cluster yang ada means di dalamnya: >" + adaMeans);
        String[] adaMeansArr = adaMeans.split(",");
        data = new String[adaMeansArr.length ][toCalculate[0].length];//array yg ada data means-nya dimasukkan ke array data
//        for (int k = 0; k < toCalculate[0].length; k++) {
//            data[0][k] = toCalculate[0][k];// <-- ini untuk alokasi smua yg di arr jadi ke array data
//        }

        //untuk apa ini?
        for (int k = 0; k < adaMeansArr.length; k++) {
            for (int l = 0; l < toCalculate.length; l++) {
                // System.out.println(getMap[k]+"<>"+(arr[l][0]));
                if (adaMeansArr[k].equals(toCalculate[l][0])) {
                    data[k ][0] = adaMeansArr[k];
                    for (int m = 1; m < toCalculate[0].length; m++) {
                        data[k ][m] = toCalculate[l][m];
                    }
                }
            }
        }
        System.out.println("=============untuk print array yang ada meansnya==================");
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[0].length; j++) {
                System.out.print(data[i][j] + "\t");
            }
            System.out.println("");
        }
        System.out.println("==============================*****================================");

        //----Untuk memindahkan isi data pada means, average value & SD pada means, average value pada data ke dalam array baru
        String[][] avgDevArr = avgDev.FindAvg(data);//the data of the choosen cluster exclude means data is located to a new array
        double[] avgArr = null;//the data of of average value for choosen cluster exclude means data is located to a new array
        double[] sdArr = null;//the data of of SD for choosen cluster exclude means data is located to a new array
        //double[] meanArr = null;//the data of Means in the choosen cluster is located to a new array
        double meanAvg = 0;//initialization
        double meanSd = 0;//initialization
        String[][] data2 = new String[data.length - 1][data[0].length - 1];//the new String data
        //ngisi data2
        for (int i = 0; i < data.length - 1; i++) {
            for (int j = 0; j < data[0].length - 1; j++) {
                data2[i][j] = data[i][j];
            }
        }
        //ngisi arrmean
        meanArr = new double[data[0].length - 1];
        for (int i = 0; i < data[0].length - 1; i++) {
            meanArr[i] = Double.parseDouble(data[data.length - 1][i + 1]);
        }

        //ngisi arrsd
        sdArr = new double[avgDevArr.length - 1];
        for (int i = 0; i < avgDevArr.length - 1; i++) {
            sdArr[i] = Double.parseDouble(avgDevArr[i + 1][avgDevArr[0].length - 1]);
        }

        //ngisi arravg
        avgArr = new double[avgDevArr.length - 1];
        for (int i = 0; i < avgDevArr.length - 1; i++) {
            avgArr[i] = Double.parseDouble(avgDevArr[i][avgDevArr[0].length - 2]);
        }

        //ngisi avgmean
        meanAvg = Double.parseDouble(avgDevArr[avgDevArr.length - 1][avgDevArr[0].length - 2]);
        //ngisi sdmean
        meanSd = Double.parseDouble(avgDevArr[avgDevArr.length - 1][avgDevArr[0].length - 1]);


        System.out.println("arrAVG=================================================");
        for (int i = 0; i < avgArr.length; i++) {
            System.out.print(avgArr[i] + " | ");
        }

        System.out.println();
        System.out.println("arrMeans=============================================");
        for (int i = 0; i < meanArr.length; i++) {
            System.out.print(meanArr[i] + " | ");
        }

        System.out.println();
        System.out.println("arrSD================================================");
        for (int i = 0; i < sdArr.length; i++) {
            System.out.print(sdArr[i] + " | ");
        }

        System.out.println();
        System.out.println("meanAVG===============================================");
        System.out.println(meanAvg);
        System.out.println("meanSD=============================================");
        System.out.println(meanSd);
        System.out.println("\n Data2: data of grade from chosen cluster to be inputted==========================");
        //move data from choosen cluster into new array to make the calculating easier
        for (int i = 0; i < data2.length; i++) {
            for (int j = 0; j < data2[0].length; j++) {
                System.out.print(data2[i][j] + "\t");
            }
            System.out.println("");
        }
        System.out.println("=== Start calculating for the correlation ===");
        double summation = 0;//summation adalah hasil sum dari (Data in Means - Avg Value of dataset Means)(data in each dataset - Avg Value of each dataset)
        correlation = new double[data2.length];//new array to store the correlation
        for (int i = 0; i < data2.length; i++) {
            double jml;//jml adalah jumlah summation per row
            summation = 0;//smmation adalah jumlah summation keseluruhan, untuk menghitung correlation yang dimasukan ke rumus

            for (int j = 0; j < data2[0].length; j++) {

                System.out.println("calculating for dataset number " + (i + 1));
                System.out.print("summation: " + summation + " | ");//hasilTambah
                System.out.println("data to be calculating:" + Double.parseDouble(data2[i][j]));
                System.out.print("avg of array [i]" + avgArr[i] + " | ");
                System.out.println("meanArr[j]: " + meanArr[j]);
                System.out.println("meanAvg: " + meanAvg);

                jml = ((meanArr[j] - meanAvg) * ((Double.parseDouble(data2[i][j])) - avgArr[i]));
                System.out.println();
                System.out.println("summation value per dataset is: " + jml);
                summation = summation + jml;
            }
            int Y; //Y adalah faktor pembagi, max value is the number of dataset - 1
            Y = data2[0].length - 1;
            System.out.println("\nY is: " + Y);

            double corrtemp = summation / sdArr[i] * meanSd * Y;//correlation sementara
            System.out.println(" The correlation for Means into this dataset= summation / Y ");
            System.out.println(summation + " / " + sdArr[i] * meanSd * Y + " = " + corrtemp);

            //mengisi hasil perhitungan correlation ke aray baru bernama correlation
            correlation[i] = corrtemp;
            System.out.println();
            System.out.println("---");
        }
        System.out.println();

        for (int i = 0; i < correlation.length; i++) {
            System.out.print("The correlation remains: " + correlation[i] + " | ");
        }
        System.out.println("\n=========================*****=============================");

        /////??????????????????????????????????????????????????????????????????????????

        // Calculating correlation finished
        String[] i = Prediction.computing(meanAvg, meanSd, data, correlation, meanArr, sdArr, avgArr);
        int x = i.length;

        for (int r = 0; r < x; r++) {
            temp = temp + "\n" + i[r];
        }
        /////??????????????????????????????????????????????????????????????????????????
        String[] y = new String[1];
        y[0] = temp;
        return y;
    }
}

我想知道是否有人可以帮助我。谢谢

2 个答案:

答案 0 :(得分:3)

如错误所示,表达式TreeApplicationSubject treeA = new TreeApplicationSubject();无法访问,因为您已从前一行的mothod return编辑。

您无法return来自java中方法的两个值[或两次]。

答案 1 :(得分:0)

噢,我的上帝......你已经在第二行返回控件第三行将如何执行。你应该把它放在if(){} else{}条件下。如下所示

  if("Some_condition){
  Tree tree = new Tree();
    return tree.hashtree(dbtoarray.getdata(), dbtoarray.GetDataPrediction());
   }
  else{
    TreeApplicationSubject treeA = new TreeApplicationSubject();
    return treeA.hashtree(dbtoarray.getdata(), dbtoarray.GetDataPrediction());
   }