简体   繁体   中英

My arraylist size is a zero even though I've added items to the it

So I tried looking up this error ( Exception in thread "main" java.lang.ArithmeticException: / by zero ) and figured that my arraylist size in the code below is zero. I can't understand why it is zero or how to solve it. Does it have to do something with the capacity of the array? I can't seem to figure out what's wrong with my code.

Btw, this code is for computing the average of all the elements in an arraylist and letting the user know what the average is.

I'm still a beginner at Java so I apologize if this may seem silly. Any help is appreciated!

import java.util.*;
public class ClassStuff {

    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        
        ArrayList <Integer> myArray = new ArrayList <Integer> ();
        int userInput = 0;
        String userConf = "";
        
        while ((!userConf.equalsIgnoreCase("y"))) {
            
            System.out.println("Please enter a number: ");
            userInput = scan.nextInt();
            
            for (int i = 1; i <= myArray.size(); i++) {
                userInput = scan.nextInt();
                myArray.add(userInput);
            }
            
            scan.nextLine();
            System.out.println("Are you done entering numbers? (y/n): ");
            userConf = scan.nextLine();
            
        }

        int result = computeAvg(myArray);
        
        System.out.println("Average is: " + result);
    }

    
    public static int computeAvg(List <Integer> myArray) {
        int sum = 0;
        int avg = 0;
        
         for (int i = 0; i < myArray.size(); i++) {               
              sum = sum + myArray.get(i);
         }
         
         return avg = sum/myArray.size();    
    }

}

I'm assuming the lines:

System.out.println("Please enter a number: ");
userInput = scan.nextInt();

gets the amount of elements you want to add to the arraylist, which we later add to the list through the for loop. In that case keep it in another variable called list_length , since userInput constantly changes in the for loop.

System.out.println("Please enter a number: ");
list_length = scan.nextInt();

Then change the for loop after this input to something like:

for(int i = 1; i <= list_length; i++) {
    userInput = scan.nextInt();
    myArray.add(userInput);
}

This is because you changed the end of the for loop to myArray.size() , but remember that it was already 0, so the for loop ends since 1 >= 0 . What you probably wanted to do was to add list_length amount of numbers into the arraylist

I found your problem.

The problem was your for loop because somehow the arrayList didn't catch any of the elements during the loop.

I also made some adjustment for the method so it counts the average correct.

Here's an example

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    ArrayList <Integer> myArray = new ArrayList<>();
    int userInput = 0;
    String userConf = "";
    
    while ((!userConf.equalsIgnoreCase("y"))) {
        System.out.println("Please enter a number: ");
        
        userInput = scan.nextInt();
        myArray.add(userInput);
        
        scan.nextLine();
        System.out.println("Are you done entering numbers? (y/n): ");
        userConf = scan.nextLine();
    }

    System.out.println(myArray);
    double result = computeAvg(myArray);
    
    System.out.println("Average is: " + result);
}


public static double computeAvg(List <Integer> myArray) {
    double sum = 0;
    double avg = 0;
    
     for (int i = 0; i < myArray.size(); i++) {               
          sum = sum + myArray.get(i);
     }
     avg =  sum / myArray.size();
     return avg;    
}

Output

myList = [4,5]

average = (4 + 5) / 2 (myArray.size())= 4.5

Hope it was useful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM