Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8825693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:56:21+00:00 2026-06-14T06:56:21+00:00

My program (included below) keeps coming up with this error that the list size

  • 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();  
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:56:22+00:00Added an answer on June 14, 2026 at 6:56 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code is pasted below.When I run this program,it keeps on calculating.I am using
I keep coming across this common error then compiling my program. I've already tried
When I try and compile this program, I get errors (included below the code)
I have this code in my program: (I included the cout statements for debugging
My program uses some external programs that i included in the Files folder. In
I have a DAL class library that is included in my program as a
Is it just to test compile a simple program, with that header file #included
I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
I am trying to understand http://beej.us/guide/bgnet/examples/select.c (included below for reference). I am doing this:
A small demo program is included below. To reproduce the problem, select a date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.