0

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);
        }
}

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

Array have the length field

ArrayList doesnot have the length field type Use size()

Comments

1

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

Comments

1
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.

Comments

0

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);
                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.