Search code examples
javafunctionmethodsoutputcall

How to get output from a function in Java?


I have a main program that call this function :

public static void eucDist(int i, double data[][], double weight[][]) {

    double sum = 0; 
    double min = 10000;

    for (int j=0; j<5; j++) {
        for (int k=0; k<5; k++) {
            sum = sum + Math.pow((weight[k][j] - data[i][k]),2);
        }

        double dist = Math.sqrt(sum);

        if(dist < min) {
            min = dist;
            int saveCol = j;
        }
    }
}

How can I get saveCol in the main program?

If I put

public int getsaveCol(){
    return saveCol;
}

so I can call saveCOl with eucDist.saveCol but it returns an error.


Solution

  • You could return it:

    public static int eucDist(int i, double data[][], double weight[][]) {
    
        double sum = 0; 
        double min = 10000;
        int saveCol = -1;
    
        for (int j=0; j<5; j++) {
            for (int k=0; k<5; k++) {
                sum = sum + Math.pow((weight[k][j] - data[i][k]),2);
            }
    
            double dist = Math.sqrt(sum);
    
            if(dist < min) {
                min = dist;
                saveCol = j;
            }
        }
        return saveCol;
    }