How to count the number of elements added to an array and find the average of numbers added in Java? -
how can number of elements in array counted?
so array can hold 10 elements, not filled. how count number of elements entered?
also, correct way find average of array? have gives me this,
the number of valid grades entered 10 [d@7ea987ac[d@7ea987acaverage: 0.0
//validate strings entered user if(convertedinput >= 0 && convertedinput <=100){ validarray[validarraycount] = convertedinput; validarraycount ++; } } catch(numberformatexception e){ } //prints number of valid values entered system.out.println("the number of valid grades entered " + varraylen); //for printing array backwards (int = (arraycount-1); i>=0; i--){ system.out.print(validarray); } //calculates sum of values in array of validarray (of grades) for(double d : validarray){ sum += d; } //avergae of valid number array double average = (sum/validarray.length); system.out.println("average: " + average); } }
thank you.
edit: input suppose 10 strings. can entered. valid strings double numbers between 0-10 can 0 , 10. strings not valid discarded. of code looks like:
import java.util.*; public class grades{ public static void main(string args[]){ int arraycount = 0; final int size = 100; int validarraycount = 0; final int validarraysize = 100; int valuesinvalidarray = 0; scanner reader = new scanner(system.in); string initialinput = new string (""); string [] sarray = new string[size]; double [] validarray = new double[validarraysize]; double sum = 0; boolean exit = false; system.out.println("you may enter 100 grades."); system.out.println("when done entering grades, press enter/return key."); //prints user. stops if nothing entered. while((arraycount < size)&&(exit == false)){ system.out.println("enter line " + (arraycount+1) + ": "); initialinput = reader.nextline(); if (initialinput.length()<1){ exit = true; } else{ sarray[arraycount]=initialinput; arraycount++; } } //convert string double try{ double convertedinput = double.parsedouble(initialinput); //validate strings entered user if(convertedinput >= 0 && convertedinput <=100){ validarray[validarraycount] = convertedinput; validarraycount ++; } } catch(numberformatexception e){ } //prints number of valid values entered system.out.println("the number of valid grades entered " + varraylen); //for printing array backwards (int = (validarraysize-1); i>=0; i--){ system.out.println(validarray[i] + ""); } //calculates sum of values in array of validarray (of grades) for(double d : validarray){ sum += d; } //avergae of valid number array double average = (sum/validarraycount); system.out.println("average: " + average); } }
validarraycount
holds number of elements entered.
instead of printing varraylen
number of valid grades entered, print out validarraycount
:
system.out.println("the number of valid grades entered " + validarraycount);
to average, can divide validarraycount
instead of validarray.length
:
double average = (sum/validarraycount);
also, have add array indices when printing array backwards:
// printing array backwards (int = validarraycount-1; i>=0; i--){ system.out.print(validarray[i] + " "); } system.out.println(); //for inserting new line after printing array elements
Comments
Post a Comment