# Thread: How can i calculate the sum of values of ArrayList in Java?

1. Member
Join Date
Feb 2013
Posts
2
Rep Power
0

## How can i calculate the sum of values of ArrayList in Java?

Hi, i need to calculate the sum of the list venta, but i know why it's not working, here is the code:

Java Code:
package sucursales;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Ventas {

private List<Integer>   venta    = null;
int                     option =      0;

public void totalventas(){
int suma=0;

for ( int i=0; i<this.venta.size(); i++)
{
suma += venta.get(i);
}

System.out.println( "La suma total de ventas es " +suma);

}

public void registro() throws IOException
{

);

System.out.println("Para finalizar el registro de ventas digite 9999");

do
{

this.venta = new ArrayList<Integer>();
System.out.println("Escriba el valor de la venta");
if (j != 9999 )
{
}
else
{
option = 9999;
}

switch(option)
{
case 1: break;
}
}while(option!=9999);

}
}
Thanks for the help!!

2. Senior Member
Join Date
Jan 2009
Location
CA, USA
Posts
271
Rep Power
8

## Re: How can i calculate the sum of values of ArrayList in Java?

It looks like you're creating a new ArrayList every iteration through the do-while loop. Try moving "this.venta = new ArrayList<Integer>();" before the "do" and see if that fixes your problem.

3. Member
Join Date
Feb 2013
Posts
2
Rep Power
0

## Re: How can i calculate the sum of values of ArrayList in Java?

Originally Posted by AndrewM16921
It looks like you're creating a new ArrayList every iteration through the do-while loop. Try moving "this.venta = new ArrayList<Integer>();" before the "do" and see if that fixes your problem.
Thanks!!!! It works now.

However, after I fill the list Venta the main appears asking me an option. And If I select the first option again I can't fill more than one number.

Here is the code of the Main:

Java Code:
package sucursales;

import java.io.IOException;

public class SucursalTest {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {

);
int option;

Sucursal sucursal = new Sucursal();
Ventas ventas = new Ventas();

do
{
System.out.println("");
System.out.println("1.Registrar las ventas"); // Fill the list Ventas //
System.out.println("2.Calcular el promedio de ventas de la empresa ");
System.out.println("3.Calcular la sucursal con el monto de ventas más bajo ");
System.out.println("4.Calcular la cantidad de sucursales con un monto de ventas mayor que el promedio de ventas ");
System.out.println("5.Finalizar ");

System.out.print("Digite la opcion: ");
System.out.println();

switch (option)
{
case 1: ventas.registro ();    break;
case 2: ventas.totalventas (); break;

}

}while (option!=5);

}

}
Thanks again. I just wanna fill the ArrayList Ventas every time that I select the option 1.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•