I have a class of a program which reads an input file and displays it. But I am trying to put the doubles it contain in two arrays and then I am trying to find the max and min of these arrays.
But the problem I am facing is that it just saves the last double contained in the file and other enteries of array is 0.0.(which should not be the case).
So here goes my code;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class DataRow2 {
static double[] fatContentA;
static double[] protienContentA;
public static void starter() {
Scanner in=new Scanner(System.in);
int numOfClusters=in.nextInt();
int numOfElementsOfDataSet=in.nextInt();
int numOfVariables=in.nextInt();
System.out.printf("%d\t %d\t %d\t",numOfClusters,numOfElementsOfDataSet,numOfVariables);
String line=in.nextLine();
String ignore=in.nextLine();
StringTokenizer ignoreLine = new StringTokenizer(ignore);
while (ignoreLine.hasMoreTokens()) {
System.out.println(ignoreLine.nextToken());
}
System.out.println(ignoreLine);
int counter=1;
while(in.hasNext()){
String readLine=in.nextLine();
Scanner parseInformation=new Scanner(readLine);
String nameOfMammal=in.next();
double fatContent=in.nextDouble();
double protienContent=in.nextDouble();
fatContentA=new double[numOfElementsOfDataSet];
protienContentA=new double[numOfElementsOfDataSet];
fatContentA[counter]=fatContent;
protienContentA[counter]=protienContent;
System.out.printf("%s\t%f\t%f\t\n",nameOfMammal,fatContent,protienContent);
counter+=1;
}
Arrays.sort(fatContentA);
int length=fatContentA.length;
//System.out.println(fatContentA[length/2]);
double max_fat = fatContentA[length-1];
double min_fat = fatContentA[0];
Arrays.sort(protienContentA);
double max_Protien = protienContentA[protienContentA.length - 1];
double min_Protien = protienContentA[0];
System.out.println("The maximum fat in this file was"+max_fat);
System.out.println("The minimum fat in this file was"+min_fat);
System.out.println("The maximum Protien in this file was"+max_Protien);
System.out.println("The maximum Protien in this file was"+min_Protien);
}
}
and this is the file I am trying to read;
7
25
2
Mammal fat_content(%) protein_content(%)
Bison 7.9 5.9
Guinea_pig 3.9 8.1
Dolphin 14.0 10.4
Donkey 1.4 1.7
Goat 4.1 3.4
Deer 19.7 9.2
Dog 8.3 9.5
Yak 6.7 5.3
Camel 3.4 3.5
Cat 10.9 11.1
Rabbit 13.1 7.1
Llama 3.2 3.9
Human 43.9 7.4
Mule 1.8 2.0
Elephant 5.0 4.0
Horse 1.3 2.1
Rat 12.6 12.3
Reindeer 20.3 10.4
Sheep 6.4 5.6
Pig 5.1 6.6
Fox 5.9 7.4
Whale 42.3 10.9
Polar_bear 31.0 10.2
Zebra 4.8 3.0
Seal 53.2 11.2
The output I get is all the file data which it was reading and:
The maximum fat in this file was53.2
The minimum fat in this file was0.0
The maximum Protien in this file was11.2
The maximum Protien in this file was0.0
which is not true because I am out printing the last two doubles of the file. The arrays only store the last two doubles on the last place of array. I dont understand where I am going wrong.
I don't know what else should I do to store the doubles in an array and find max and min... Thanks in advance for help and it would be highly appreciated.