Search code examples
javaarraylistjtextfield

How to take the size of an ArrayList<double> and set it into a jTextField so i can see it


I have this arraylist which is filled with numbers that are added from a text file we open

private ArrayList< Double> data = new ArrayList< Double>();
...

if( jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ) {

    String file = jfc.getSelectedFile().getPath();
    String line = null;
    String[] ch;

    try {   
        FileReader fr = new FileReader( file);
        BufferedReader br = new BufferedReader(fr);
        data.clear();
        while( (ligne=br.readLine())!=null ) {
            ch = line.split( ";" );
            for (int i = 0; i < ch.length; i++) {
                data.add( Double.parseDouble(ch[i]) );  
            }                       
        }

        br.close();             
    }
    catch( IOException ioe ) {
    }           
}

How can i put the size of data(data.size()) to show in a jTextField named(jtfN)? also how can I calculate the average of the data? thanks


Solution

  • Set the jTextField:

    jtfN.setText(String.valueOf(data.size()))

    Calculate the average:

    double total = 0;
    /* summ all doubles */
    for(double d : data) {
        total += d;
    }
    /* devide by count of doubles */
    double average = total / data.size();