0

My program (included below) keeps coming up with this error that the list size is zero, and that's messing up everything else. I'm guessing that means I didn't add the elements in correctly, but I can't figure out what's wrong with it. I'm technologically challenged, so I'm sorry that this isn't formatted right. Thanks for your help!

import java.util.Scanner;
import java.util.ArrayList;
public class ListStat {
    public static void main (String[] args) {
        ListStat r = new ListStat();
        System.out.println("A default list was created." + r.toString());
        ListStat r1 = new ListStat(30);
        System.out.println("Another list was created." + r1.toString());
        ListStat r2 = new ListStat(40);
        System.out.println("Another list was created." + r2.toString());
        r1.add(4); 
        System.out.println("The item at index 13 for r1 is: " + r1.get(13));
        System.out.println("r1 and r2 are equal: " + r1.equals(r2));
        System.out.println("r1 and r2 overlap this number of times: " + r1.countOverlap(r1, r2));
    }

private ArrayList<Integer> list;

public ListStat() {
list = new ArrayList<Integer>();
}

public ListStat(int N) {
list = new ArrayList<Integer>(N);
for (int i=0; i<N; i++) {
    list.add(i, i+1);
}
}

public void add(int num) {
list.add(num);
}

public int get(int index) {
return list.get(index);
}

public int getListLength(){
return list.size();
}

public double getMean() {
int sum = 0;
for (int i=0; i<list.size(); i++) {
    sum = sum + list.get(i);
}
double mean = sum/list.size();
return mean;
}

public int getSum() {
int sum = 0;
for (int i=0; i<list.size(); i++) {
    sum = sum + list.get(i);
}
return sum;
}

public boolean equals(ListStat r) {
if (r.equals(list))
return true;
else
return false;
}

public static int countOverlap(ListStat r1, ListStat r2) {
int count = 0;
for (int i=0; i<r1.getListLength(); i++) {
    for (int j=0; j<r2.getListLength(); j++) {
        if (r1.get(i)==r2.get(j)) {
            count++;
        }
    }
}
return count;
}

public double getStdDev() {
int sum = 0;
double standardDev = 0.0;
for (int i=0; i<list.size(); i++) {
    sum = sum + list.get(i);
}
double mean = sum/list.size();
double standsum=0;
for (int i=0; i<list.size(); i++) {
    standardDev = (list.get(i) - mean) * (list.get(i) - mean);
    standsum=standsum+standardDev;
}
standsum = standsum/list.size();
standsum = Math.sqrt(standsum);
return standsum;
}

public String toString() {
return "The list length is " + getListLength() + ", the mean is " + getMean() + ", the sum is " + getSum() + ", and the standard deviation is " + getStdDev();  
}
}
4
  • How does that even compile? I'm guessing you have problems with the scope of list, but since that code is nothing like valid, it's hadr to tell... Commented Nov 13, 2012 at 3:03
  • 1
    Hi, pls add your "main" code where you call your class methods. Commented Nov 13, 2012 at 3:05
  • Thanks! I added everything now, I'm a bit technologically challenged haha Commented Nov 13, 2012 at 3:32
  • What are the current and expended outputs? Commented Nov 13, 2012 at 3:47

2 Answers 2

2

The problem is caused by

double mean = sum / list.size();

in getMean() method.

  1. The program create an object r, which is empty, means

    r.size() = 0;

  2. Then at line 9, call r.toString() method, this method invoke getMean(). Since r.size is 0, so the program throw an exception

Exception in thread "main" java.lang.ArithmeticException: / by zero

Sign up to request clarification or add additional context in comments.

Comments

2

In your main method,

First you invoke the no-arg constructor

ListStat r = new ListStat();    

And following is how your no-arg constructor looks like:

public ListStat() {
    list = new ArrayList<Integer>();
}

It initializes the list but doesn't add anything to the list, so the list size is 0.

Second, is the following

System.out.println("A default list was created." + r.toString());

which is invoking the toString method.

If you look at the toString method

public String toString() {
    return "The list length is " + getListLength() + ", the mean is " + getMean() + ", the sum is " + getSum() + ", and the standard deviation is " + getStdDev();
}

getMean method is what you are calling there, and the following is something that you are doing in that method:

double mean = sum / list.size();

The list size is 0 there.

So, you probably want to add a condition-check there, to see if the list size is 0, before doing that.

6 Comments

I'm sorry, I'm pretty confused, I don't understand what you're trying to tell me
Ok, so what I'm trying to do there is have the size, which should be 30 for r1 and 40 for r2, so how do I do that? I don't understand why it's 0
ListStat r = new ListStat(); <<< this creates a list of size zero!
Actually all the constructors create a list of size 0, but others add elements to the list, which increments the size of the list.
I know, but that particular constructor was the one that had the list at size zero when getMean was called. Anyway you have pointed out in your answer already. I was a bit too late :P
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.