Results 1 to 3 of 3
  1. #1
    ambigus9 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Question 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.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Ventas {
    
    	private BufferedReader  lector   = null;
    	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
    	{
    		
    		lector = new BufferedReader(
    	            new InputStreamReader(System.in)
    	          );
    
    		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");
    			int j = Integer.parseInt(lector.readLine());
    			if (j != 9999 )
    			{
    				venta.add(j);
    			}
    			else
    			{
    				option = 9999;
    			}
    			
    			
    			switch(option)
    			{
    			case 1: break;
    			}
    		}while(option!=9999);
    		
    	}   
    }
    Thanks for the help!!

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default 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. #3
    ambigus9 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

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

    Quote Originally Posted by AndrewM16921 View Post
    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.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class SucursalTest {
    
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException {
    		
    	      BufferedReader lector = new BufferedReader(
                      new InputStreamReader(System.in)
                      );
    	      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: ");
    	          option = Integer.parseInt(lector.readLine());
    	          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.

Similar Threads

  1. Replies: 4
    Last Post: 03-21-2012, 08:27 PM
  2. To display arraylist values in jsp
    By manideep.d132 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-09-2011, 02:59 AM
  3. ArrayList find values
    By ralf in forum New To Java
    Replies: 39
    Last Post: 07-06-2011, 11:08 PM
  4. trying to set() values of in list of arraylist
    By alvations in forum New To Java
    Replies: 15
    Last Post: 10-13-2008, 09:35 PM
  5. Replies: 40
    Last Post: 07-04-2008, 08:41 AM

Posting Permissions

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