DevanDev
DevanDev

Reputation: 355

Java: Determining the position of the value in CSV file

I have a CSV file : of DOSE Computation values with 80 rows and 80 columns, I have written the code to find the maximum Value in CSV file as follows

public double getMaxDose()
    {
        double dose=0.0;
        for(DetEl det_el:det_els)
        {
            if (det_el.getDose()>dose)
                dose=det_el.getDose();
        }
        return dose;
    }

I would like to find the position of the maximum value in a file of 80 rows and columns, i would like to know in which row and Column the maximum value exists

Any hints greatly appreciated

Upvotes: 1

Views: 435

Answers (1)

bprasanna
bprasanna

Reputation: 2453

You need to use a variable to get the position of maximum value, we can use that position value to obtain the row and column values.

Following program may throw some light on the logic:

public class Dose {
   public static void main(String... args) {
     double[][] arr = new double[][] {
                    {0.0,1.0,3.0},
                    {2.0,55.0,8.0},
                    {98.0,9.0,67.0},
                    {7.0,-1.0,22.0}
                    };
     double dose=0.0;
     int maxPosition=0;
     int counter = 0;
     for(double[] tarr:arr)
     {
        for(double aval:tarr) {
           counter++;
           if (Double.compare(aval,dose) > 0) {
              dose=aval;
              maxPosition=counter;
           }
        }
     }
     System.out.println("Maximum value: "+dose);
     System.out.println("Max value position: "+maxPosition);
     System.out.println("Arr length: "+arr.length);
     System.out.println("Sub Arr length: "+arr[0].length);
     System.out.println("Row:"+((maxPosition/arr.length)+1) +
                        " Column:"+(maxPosition%arr[0].length==0?arr[0].length:maxPosition%arr[0].length));
   }
}

Upvotes: 1

Related Questions