mercredi 25 février 2015

counting elements in an array imported from a data file



I am writing a program that will import values from a txt file in to an array, I then need to count how many of those elements are greater than or equal to 36. The data imports fine, and the total amount of values it displays is correct, but I can not get it display the amount of times the number 36 is found in the file. Thanks for any help!



public static void main(String[] args) throws Exception {
int[] enrollments = new int [100];
int count;
int FullClass;
double ClassPercentage;

return count (number of data items)
count = CreateArray(enrollments);
System.out.println (count );


FullClass = AddValues (enrollments);
System.out.println (FullClass)
ClassPercentage= FullClass/count;
System.out.print(ClassPercentage +"% of classes are full");


}//end main

/**
*
* @param classSizes
*/
public static int CreateArray(int[] classSizes) throws Exception{


int count = 0;

File enrollments = new File("enrollments.txt");
Scanner infile = new Scanner (enrollments);

while (infile.hasNextInt()){
classSizes[count] = infile.nextInt();
count++}//end while
return count; //number of items in an array

} // end CreateArray
/**************************************************************************/

/**
*
* @throws java.lang.Exception
*/
public static int AddValues (int[] enrollments) throws Exception{
int i;// array index
int FullClass =0 ; // will count the class with 36 students
int ClassPercentage;

int count = CreateArray(enrollments);

for(i=0; i== count; i++){
if (count >=36 ){
FullClass++;
System.out.println(FullClass);
}// end for
}
return FullClass;


}// end AddValues
}//end main



Aucun commentaire:

Enregistrer un commentaire