import java.io.*;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.*;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Animesh Pandey
 */

public class Main {
    public static int [][] sort( int a[][]){
        int i, j, t = 0, t1 = 0, t2 = 0 ;
        for(i = 0; i < a.length; i++){
            for(j = 1; j < (a.length - i); j++){
                if(a[j-1][2] > a[j][2]){
                    t = a[j-1][2];
                    a[j-1][2] = a[j][2];
                    a[j][2] = t;

                    t1 = a[j-1][1];
                    a[j-1][1] = a[j][1];
                    a[j][1] = t1;

                    t2 = a[j-1][0];
                    a[j-1][0] = a[j][0];
                    a[j][0] = t2;
                }
            }
        }
        return a;
    }

    public static void main(String args[])
            throws IOException {
                String thisLine;
                String[] fullText = new String[100]; //number of possible lines in your file
                int counter = 0;
                int valueForPrediction = 1;
                int K = 8;

                int [][] Matrix = new int [20][3];
                
                List<Integer> Height   = new LinkedList<Integer>();
                List<Integer> OP       = new LinkedList<Integer>();
                List<Integer> Distance = new LinkedList<Integer>();

                String fName = "F:/Lab4.csv";
                String opfName = "F:/Lab4_1.csv";

                FileInputStream fis     = new FileInputStream(fName);
                FileOutputStream fos    = new FileOutputStream(opfName);
                BufferedReader myInput  = new BufferedReader(new InputStreamReader(fis));
                BufferedWriter myOutput = new BufferedWriter(new OutputStreamWriter(fos));

                while ((thisLine = myInput.readLine()) != null) {
                    int sPos = 0;
                    int ePos = 1;
                    int tDelim = 0;
                    String d = "";
                    int colCount = 0;
                    int collCount = 0;
                    boolean fMe = false;
                    boolean finalCol = false;
                    while ( ePos < thisLine.length()) {
                        sPos = thisLine.indexOf(",", ePos);
                        tDelim = thisLine.indexOf(",\"", ePos);

                        if (tDelim == ePos) {
                            fMe = true;
                            collCount = ePos + 1;
                            while (collCount <= thisLine.indexOf("\",")) {
                                collCount += 1;
                            }
                            colCount += 1;
                            sPos = collCount;
                        }

                        if (colCount == 47) { //Get Max count of columns
                            finalCol = true;
                            collCount = ePos + 1;
                            while (collCount <= thisLine.lastIndexOf("\"")) {
                                collCount += 1;
                            }
                            colCount += 1;
                            sPos = collCount;
                        }

                        if (sPos == -1) {
                            d = thisLine.substring(ePos,thisLine.length());
                            //System.out.print(d);

                            if(d.equals("m")){
                                myOutput.write("1");
                                OP.add(1);
                            }else{
                                myOutput.write("2");
                                OP.add(2);
                            }
                            break;
                        }else if(ePos == 1) {
                            d = thisLine.substring(ePos-1,sPos);
                            //System.out.print(d+",");
                            if(d.equals("medium")){
                                myOutput.write("2,");
                                Height.add(2);
                            }else if(d.equals("tall")){
                                myOutput.write("1,");
                                Height.add(1);
                            }else{
                                myOutput.write("3,");
                                Height.add(3);
                            }
                        }else{
                            if (fMe) {
                                d = thisLine.substring(ePos + 1, sPos);
                                fMe = false;
                            }else if (finalCol) {
                                d = thisLine.substring(ePos, sPos);
                                finalCol = false;
                            }else{
                                d = thisLine.substring(ePos, sPos);
                            }
                            //System.out.print(d+",");
                            if(d.equals("medium")){
                                myOutput.write("2,");
                                Height.add(2);
                            }else if(d.equals("tall")){
                                myOutput.write("1,");
                                Height.add(1);
                            }else{
                                myOutput.write("3,");
                                Height.add(3);
                            }
                        }
                        colCount += 1;
                        ePos = sPos + 1;
                    }
                    //System.out.println("\n");
                    myOutput.write("\n");
                   
        counter++;
        fullText[counter] = thisLine;

    }
               Integer[] Hgt = Height.toArray(new Integer[0]);
               Integer[] Output = OP.toArray(new Integer[0]);
               int Range = Hgt.length;
               for(int i=0; i<Range; ++i){
                    Distance.add(Math.abs(valueForPrediction - Hgt[i]));
               }
               //System.out.println(Distance);
               Integer[] Dist = Distance.toArray(new Integer[0]);
               /*Dist = sort(Dist);
               for(int i=0; i<Range; ++i){
                    System.out.print(Dist[i]);
               }*/
               System.out.println("\n");

               for(int n=0; n<Range; ++n){
                   Matrix[n][0] = Hgt[n];
                   Matrix[n][1] = Output[n];
                   Matrix[n][2] = Dist[n];
               }
               int [][] sortedMatrix = sort(Matrix);

               for(int i=0; i<Range; ++i){
                   for(int j=0; j<3; ++j){
                       System.out.print(sortedMatrix[i][j]);
                   }
                   System.out.println("\n");
               }
               int sum = 0;
               float average;
               for(int i=0; i<K; ++i){
                   sum += sortedMatrix[i][1];
               }
               average = sum/K;
               System.out.println(sum + "\n");
               //The sum is 11 and K is 8. So, the value of average should be 1.375
               System.out.printf("%f", average);
      myOutput.close();
  }
}

The csv files are in the attachment.

The average is supposed to be greater than 1.
But still using System.out.printf() prints it as 1.00000.

What should I do??? :?:

Please help!

Check that you are not doing integer division.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.