user3451158
user3451158

Reputation: 43

.length error when trying to use a for loop with an ArrayList

First off I'm sorry for the title I couldn't think of a better way to word it. The actual error is in option 3(whenever I try to add together all the sales in option 1). When I try to use salesList.length to track the size of the array I get cannot find symbol- variable length I'm very new to using array lists and that method worked for me in an earlier array but that array wasn't dynamic. Is there a specific way to track the length of a dynamic array list?

import java.util.*;
public class CustomerTest
{
public static void main(String[] args)
        {
            double totalSales = 0;
            ArrayList<String> nameList;
            nameList = new ArrayList<String>();
            ArrayList<Double> salesList;
            salesList = new ArrayList<Double>();
            Scanner myScanner = new Scanner(System.in);
            boolean done = true;     
            do
            {

                System.out.println("1) Add a new customer \n 2) Print all customers \n 3) Compute and print the total sales \n 4) Quit");
                int choice = Integer.parseInt(myScanner.nextLine());
                if (choice == 1)
                {
                    System.out.print("Add a new customer ");
                    String answer = myScanner.nextLine();
                    nameList.add(answer);
                    System.out.print("Enter their sales ");
                    String answer2 = myScanner.nextLine();
                    double answer3 = Double.parseDouble(answer2);
                    salesList.add(answer3);
                }
                else if(choice == 2)
                {
                    System.out.println("Customers: " + nameList);
                    System.out.println("Sales: " + salesList);
                }
                else if(choice == 3)
                {
                    for(int i = 0; i < salesList.length; i++)
                    {
                     totalSales = totalSales + salesList[i];   
                    } 
                    System.out.println(totalSales);
                }
                else if(choice == 4)
                {
                    System.out.println("Goodbye   *Bows gracefully*"); 
                    done = false;
                }
                else
                    System.out.println("Invalid Choice");     
            }
            while (done);
           System.exit(0);
        }
}

Upvotes: 0

Views: 235

Answers (5)

Raju Sharma
Raju Sharma

Reputation: 2516

your code is having bugs: Change the else if(choice==3) {} conditional part to following. you cannot use salesList.length, it can be done using salesList.size() and pleas change salesList[i] to salesList.get(i).

 else if(choice == 3)
                {
                    for(int i = 0; i < salesList.size(); i++)
                    {
                     totalSales += salesList.get(i);   
                    } 
                    System.out.println(totalSales);
                }

Upvotes: 0

CodeCamper
CodeCamper

Reputation: 6980

else if (choice == 3) {
        for (int i = 0; i < salesList.size(); i++) {
            totalSales += salesList.get(i);
        }
        System.out.println(totalSales);
        }

Replace choice 3 with this and it should work.

Upvotes: 1

stinepike
stinepike

Reputation: 54682

use salesList.size(). unlike arrays you cannot use salesList.lenght

Upvotes: 1

Nambi
Nambi

Reputation: 12042

Array have the length field

ArrayList doesnot have the length field type Use size()

Upvotes: 1

Azar
Azar

Reputation: 1106

Change it to salesList.size();. Unlike arrays, the length of an ArrayList is not a directly accessible field.

Upvotes: 1

Related Questions