-3

I am currently making a simulator for fake basketball players but am currently stuck on finding the mean of all of the numbers in the ArrayList.

I know how to add all of the values up but when ever I try to make a counter variable to divide the list or use list.getSize(), none of them work.

Hopefully this makes sense to you, I am happy to explain the problem in more detail.

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Application {
    private JFrame frame;
    private JTextField pst;
    private JTextField pg;
    private JTextField sg;
    private JTextField pf;
    private JTextField C;
    private JTextField sf;
    private JTextField txtPlayerName;
    private JTextField ED;
    private JTextField CJ;
    private JTextField SB;
    private JTextField SW;
    private JTextField JO;
    private JTextField num;
    private JTextField edNum;
    private JTextField cjNum;
    private JTextField sbNum;
    private JTextField swNum;
    private JTextField joNum;
    private JTextField ppg;
    private JTextField ppgED;
    private JTextField ppgCJ;
    private JTextField ppgSB;
    private JTextField ppgJO;
    private JTextField ppgSW;
    private JButton btnPlay;
    private List<Integer> ppgList;
    private int sum;
    private int counter;

    // Launch the application.
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Application window = new Application();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Application() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        initComponenets();
        createEvents();
    }

    ////////////////////////////////////////////////////////////////////////
    //Contains all of the code for creating events
    private void createEvents() {

    }
    //Contains all of the code for creating and initializing components
    private void initComponenets() {

        ////////////////////////////////////////////////////////////Frame
        frame = new JFrame();
        frame.setTitle("Title");
        frame.setBounds(100, 100, 801, 450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        ////////////////////////////////////////////////////////////Title
        JTextPane title = new JTextPane();
        title.setBackground(Color.LIGHT_GRAY);
        title.setEditable(false);
        title.setFont(new Font("Tahoma", Font.PLAIN, 20));
        title.setForeground(Color.DARK_GRAY);
        title.setBounds(237, 11, 296, 50);
        title.setText("   Basketball Simulation Game");
        frame.getContentPane().add(title);

    /////////////////////////////////////////////////////////// Position TextBox
        pst = new JTextField();
        pst.setHorizontalAlignment(SwingConstants.CENTER);
        pst.setEditable(false);
        pst.setFont(new Font("Tahoma", Font.BOLD, 14));
        pst.setText("PST");
        pst.setBounds(10, 64, 50, 35);
        frame.getContentPane().add(pst);
        pst.setColumns(10);

        pg = new JTextField();
        pg.setHorizontalAlignment(SwingConstants.CENTER);
        pg.setText("PG");
        pg.setFont(new Font("Tahoma", Font.BOLD, 14));
        pg.setEditable(false);
        pg.setColumns(10);
        pg.setBounds(10, 99, 50, 50);
        frame.getContentPane().add(pg);

        sg = new JTextField();
        sg.setHorizontalAlignment(SwingConstants.CENTER);
        sg.setText("SG");
        sg.setFont(new Font("Tahoma", Font.BOLD, 14));
        sg.setEditable(false);
        sg.setColumns(10);
        sg.setBounds(10, 148, 50, 50);
        frame.getContentPane().add(sg);

        sf = new JTextField();
        sf.setText("SF");
        sf.setHorizontalAlignment(SwingConstants.CENTER);
        sf.setFont(new Font("Tahoma", Font.BOLD, 14));
        sf.setEditable(false);
        sf.setColumns(10);
        sf.setBounds(10, 196, 50, 50);
        frame.getContentPane().add(sf);

        pf = new JTextField();
        pf.setText("PF");
        pf.setHorizontalAlignment(SwingConstants.CENTER);
        pf.setFont(new Font("Tahoma", Font.BOLD, 14));
        pf.setEditable(false);
        pf.setColumns(10);
        pf.setBounds(10, 242, 50, 50);
        frame.getContentPane().add(pf);

        C = new JTextField();
        C.setText("C");
        C.setHorizontalAlignment(SwingConstants.CENTER);
        C.setFont(new Font("Tahoma", Font.BOLD, 14));
        C.setEditable(false);
        C.setColumns(10);
        C.setBounds(10, 291, 50, 50);
        frame.getContentPane().add(C);


        /////////////////////////////////////////////// PlayerName
        txtPlayerName = new JTextField();
        txtPlayerName.setText("Player Name");
        txtPlayerName.setHorizontalAlignment(SwingConstants.CENTER);
        txtPlayerName.setFont(new Font("Tahoma", Font.BOLD, 14));
        txtPlayerName.setEditable(false);
        txtPlayerName.setColumns(10);
        txtPlayerName.setBounds(59, 64, 95, 35);
        frame.getContentPane().add(txtPlayerName);

        ED = new JTextField();
        ED.setText("ED");
        ED.setHorizontalAlignment(SwingConstants.CENTER);
        ED.setFont(new Font("Tahoma", Font.BOLD, 14));
        ED.setEditable(false);
        ED.setColumns(10);
        ED.setBounds(59, 99, 95, 50);
        frame.getContentPane().add(ED);

        CJ = new JTextField();
        CJ.setText("CJ");
        CJ.setHorizontalAlignment(SwingConstants.CENTER);
        CJ.setFont(new Font("Tahoma", Font.BOLD, 14));
        CJ.setEditable(false);
        CJ.setColumns(10);
        CJ.setBounds(59, 148, 95, 50);
        frame.getContentPane().add(CJ);

        SB = new JTextField();
        SB.setText("SB");
        SB.setHorizontalAlignment(SwingConstants.CENTER);
        SB.setFont(new Font("Tahoma", Font.BOLD, 14));
        SB.setEditable(false);
        SB.setColumns(10);
        SB.setBounds(59, 196, 95, 50);
        frame.getContentPane().add(SB);

        SW = new JTextField();
        SW.setText("Swanigan");
        SW.setHorizontalAlignment(SwingConstants.CENTER);
        SW.setFont(new Font("Tahoma", Font.BOLD, 14));
        SW.setEditable(false);
        SW.setColumns(10);
        SW.setBounds(59, 245, 95, 47);
        frame.getContentPane().add(SW);

        JO = new JTextField();
        JO.setText("Jordan");
        JO.setHorizontalAlignment(SwingConstants.CENTER);
        JO.setFont(new Font("Tahoma", Font.BOLD, 14));
        JO.setEditable(false);
        JO.setColumns(10);
        JO.setBounds(59, 291, 95, 50);
        frame.getContentPane().add(JO);

        /////////////////////////////////////////////Numbers
        num = new JTextField();
        num.setText("#");
        num.setHorizontalAlignment(SwingConstants.CENTER);
        num.setFont(new Font("Tahoma", Font.BOLD, 14));
        num.setEditable(false);
        num.setColumns(10);
        num.setBounds(153, 64, 50, 35);
        frame.getContentPane().add(num);

        edNum = new JTextField();
        edNum.setText("0");
        edNum.setHorizontalAlignment(SwingConstants.CENTER);
        edNum.setFont(new Font("Tahoma", Font.BOLD, 14));
        edNum.setEditable(false);
        edNum.setColumns(10);
        edNum.setBounds(153, 99, 50, 50);
        frame.getContentPane().add(edNum);

        cjNum = new JTextField();
        cjNum.setText("3");
        cjNum.setHorizontalAlignment(SwingConstants.CENTER);
        cjNum.setFont(new Font("Tahoma", Font.BOLD, 14));
        cjNum.setEditable(false);
        cjNum.setColumns(10);
        cjNum.setBounds(153, 148, 50, 50);
        frame.getContentPane().add(cjNum);

        sbNum = new JTextField();
        sbNum.setText("24");
        sbNum.setHorizontalAlignment(SwingConstants.CENTER);
        sbNum.setFont(new Font("Tahoma", Font.BOLD, 14));
        sbNum.setEditable(false);
        sbNum.setColumns(10);
        sbNum.setBounds(153, 196, 50, 50);
        frame.getContentPane().add(sbNum);

        swNum = new JTextField();
        swNum.setText("8");
        swNum.setHorizontalAlignment(SwingConstants.CENTER);
        swNum.setFont(new Font("Tahoma", Font.BOLD, 14));
        swNum.setEditable(false);
        swNum.setColumns(10);
        swNum.setBounds(153, 245, 50, 47);
        frame.getContentPane().add(swNum);

        joNum = new JTextField();
        joNum.setText("23");
        joNum.setHorizontalAlignment(SwingConstants.CENTER);
        joNum.setFont(new Font("Tahoma", Font.BOLD, 14));
        joNum.setEditable(false);
        joNum.setColumns(10);
        joNum.setBounds(153, 291, 50, 50);
        frame.getContentPane().add(joNum);

        //////////////////////////////////Points per game
        ppg = new JTextField();
        ppg.setText("PPG");
        ppg.setHorizontalAlignment(SwingConstants.CENTER);
        ppg.setFont(new Font("Tahoma", Font.BOLD, 14));
        ppg.setEditable(false);
        ppg.setColumns(10);
        ppg.setBounds(201, 64, 50, 35);
        frame.getContentPane().add(ppg);

        ppgED = new JTextField();
        ppgED.setText("0.0");
        ppgED.setHorizontalAlignment(SwingConstants.CENTER);
        ppgED.setFont(new Font("Tahoma", Font.BOLD, 14));
        ppgED.setEditable(false);
        ppgED.setColumns(10);
        ppgED.setBounds(201, 99, 50, 50);
        frame.getContentPane().add(ppgED);

        ppgCJ = new JTextField();
        ppgCJ.setText("0.0");
        ppgCJ.setHorizontalAlignment(SwingConstants.CENTER);
        ppgCJ.setFont(new Font("Tahoma", Font.BOLD, 14));
        ppgCJ.setEditable(false);
        ppgCJ.setColumns(10);
        ppgCJ.setBounds(201, 148, 50, 50);
        frame.getContentPane().add(ppgCJ);

        ppgSB = new JTextField();
        ppgSB.setText("0.0");
        ppgSB.setHorizontalAlignment(SwingConstants.CENTER);
        ppgSB.setFont(new Font("Tahoma", Font.BOLD, 14));
        ppgSB.setEditable(false);
        ppgSB.setColumns(10);
        ppgSB.setBounds(201, 196, 50, 50);
        frame.getContentPane().add(ppgSB);

        ppgJO = new JTextField();
        ppgJO.setText("0.0");
        ppgJO.setHorizontalAlignment(SwingConstants.CENTER);
        ppgJO.setFont(new Font("Tahoma", Font.BOLD, 14));
        ppgJO.setEditable(false);
        ppgJO.setColumns(10);
        ppgJO.setBounds(201, 291, 50, 50);
        frame.getContentPane().add(ppgJO);

        ppgSW = new JTextField();
        ppgSW.setText("0.0");
        ppgSW.setHorizontalAlignment(SwingConstants.CENTER);
        ppgSW.setFont(new Font("Tahoma", Font.BOLD, 14));
        ppgSW.setEditable(false);
        ppgSW.setColumns(10);
        ppgSW.setBounds(201, 245, 50, 47);
        frame.getContentPane().add(ppgSW);

        ////////////////////////////////////////Play JButton
        btnPlay = new JButton("PLAY");
        btnPlay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                getPPG(33, ppgED);
            }
        });
        btnPlay.setFont(new Font("Tahoma", Font.PLAIN, 24));
        btnPlay.setBounds(304, 344, 160, 56);
        frame.getContentPane().add(btnPlay);




    }

    public void getPPG(int num, JTextField box){

        Random rand = new Random();
        String text;
        ppgList = new ArrayList<Integer>();

        for(int xy = 0; xy < 1; xy++){

            num = num + rand.nextInt(10);
            ppgList.add(num);

        }

        for(int i = 0; i < ppgList.size(); i++)
        {
            counter++;
            sum = sum + ppgList.get(i) / counter;
        }
        Integer.toString(sum);
        text = String.valueOf(sum);
        box.setText(text);
    }
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • 1
    By the way the code that doesn't work is in the getPPG() method on the bottom of the page – Anthony.Orloff Aug 23 '17 at 02:13
  • 1
    Way too much, no problem description. – shmosel Aug 23 '17 at 02:16
  • 2
    HINT: the mean (also called "average") of _n_ numbers is the sum of all those numbers divided by _n_. – Kevin Anderson Aug 23 '17 at 02:25
  • 2
    Getting the average of a list of numbers has ***nothing*** to do with Swing. Also, don't inflict 'code dumps' on us. Prepare and post a [mcve] that is **specific* to the problem (which could be done using a command line app. that has a list of numbers written in or generated by the code). Removed Swing tag. – Andrew Thompson Aug 23 '17 at 02:36
  • Possible duplicate of [Calculating average of an array list?](https://stackoverflow.com/questions/10791568/calculating-average-of-an-array-list) – Zabuzard Aug 23 '17 at 02:56
  • Kevin Anderson I think I know what mean is by the way, your comment is irrelevant. And Andrew Thompson why did you even comment about that and it technically is swing but not a swing issue. – Anthony.Orloff Aug 23 '17 at 23:33
  • @Anthony.Orloff If an user answered your question please also accept his answer ([Accepting Answers: How does it work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)). If not than please specify what remains unanswered, this is a really crucial part of StackOverflow, thank you very much. – Zabuzard Sep 24 '17 at 15:45
  • @Zabuza yeah buddy that's what I did down there and I really like the smart ass response by the way :D – Anthony.Orloff Sep 25 '17 at 21:10

1 Answers1

2

Your current approach adds an item divided by the current counter which is not correct. Take a look at the following example:

values:      1, 2, 3, 4, 5
average:     3

your result:   1/1 + 2/2 + 3/3 + 4/4 + 5/5
             = 1 + 1 + 1 + 1 + 1
             = 5

The correct approach first computes the overall sum of all values and then divides by the amount of items.

correct:  (1 + 2 + 3 + 4 + 5) / 5
         = 15 / 5
         = 3

Or alternatively you could apply the distributive property of division and addition and obtain the equivalent expression:

1/5 + 2/5 + 3/5 + 4/5 + 5/5

However you should stay as long as possible at int to avoid precision problems and exploit the heavy integer optimizations of the JVM.


You could either compute the average on your own or simply use methods from the Java library like IntStream#average:

// Setup the list
Random rnd = new Random();
int amount = 1_000;
int valueUpperBound = 10;

ArrayList<Integer> values = new ArrayList<>();
for(int i = 0; i < amount; i++){
    values.add(rnd.nextInt(valueUpperBound));
}

// Compute the average
double avg = values.stream()
    .mapToInt(Integer::intValue)
    .average()
    .getAsDouble();

The code uses your list as source for a stream, then it maps each value Integer to an int using the Integer#intValue method in order to obtain an IntStream (documentation). This class offers methods like IntStream#average which returns an OptionalDouble and the OptionalDouble#getAsDouble method returns the result (for details take a look at the documentation).


A correct by-hand average computation looks like (from here: SO/a/10791597/2411243):

private double calculateAverage(List<Integer> values) {
    Integer sum = 0;
    if(!values.isEmpty()) {
        for (Integer value : values) {
            sum += value;
        }
        return sum.doubleValue() / values.size();
    }
    return sum;
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82