0

A supermarket wants to reward the top customers of the day, that is, the topN customers with the largest sales, where topN is a value that the user of the program supplies, showing the customer’s name on a screen in the supermarket. For that purpose, the customer’s purchase amount is stored in an ArrayList

Implement a method: public static ArrayList

Write a program that prompts the cashier to enter all prices and names, adds them to two array lists, calls the method that you implemented, and displays the result. Use a price of 0 as a sentinel.

My errors when compiling are:

------ Compile ----------
hw1num2.java:44: error: cannot find symbol
                double check = iter.nextDouble();
                                   ^
  symbol:   method nextDouble()
  location: variable iter of type Iterator
hw1num2.java:45: error: cannot find symbol
                if (check>=sorted(topN-1))
                           ^
 symbol:   method sorted(int)
location: class hw1num2
hw1num2.java:47: error: no suitable method found for add(double)
                    topCust.add(check);
                           ^
   method ArrayList.add(int,String) is not applicable
    (actual and formal argument lists differ in length)
 method ArrayList.add(String) is not applicable
 (actual argument double cannot be converted to String by method invocatio>n conversion)
3 errors

Output completed (1 sec consumed) - Normal Termination

Here's what I have:

import java.util.*;

public class hw1num2
{
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        ArrayList<Double> sales = new ArrayList<Double>();
        ArrayList<String> customers = new ArrayList<String>();

        boolean end = true;

        while (end)  //Continues to take input for customer names and purchases until done
        {
            System.out.println("Enter the first name of the customer.");
            String cust = in.next();
            System.out.println("Enter the purchase total of that customer.");
            Double total = in.nextDouble();
            sales.add(total);
            customers.add(cust);
            System.out.println("Enter 1 if you have another customer to add, or 2 if you are done.");
            int choice = in.nextInt();
            if (choice != 1)
            {
                end=false;
            }
        }
        System.out.println("How many top customers would you like to display?");
        int topN = in.nextInt();
        System.out.println(nameOfBestCustomers(sales, customers, topN));  //calls method that computes top custs
    }

    public static ArrayList<String> nameOfBestCustomers(ArrayList<Double> sales, ArrayList<String> customers, 
    int topN) //Finds out who the topN customers were
    {
        ArrayList<Double> sorted = new ArrayList<Double>(sales); //create copy of sales ArrayList
        ArrayList<String> topCust = new ArrayList<String>(); //create ArrayList to hold top cust names
        Collections.sort(sorted);  //sort the copied ArrayList.
        Iterator iter = sales.iterator();  
        while (iter.hasNext())  //iterate through sales ArrayList to find indexes of top purchases
        {
            for (int i=0;i<sales.size();i++)
            {
                double check = (double)iter.next(); 
                if (check>=sorted.get(topN-1)) //checks if each index is >= the top Nth customer
                {
                    topCust.add(customers.get(i)); //if so, adds it to topCust list
                }
            }
        }
        return topCust;  //returns the list with the top customer names
    }
}

1 Answer 1

1

1- You are getting the error because the Iterator class doesn't have a method called nextDouble(). Please have a look at Iterator API for a list of supported methods.

2- topCust is a String ArrayList, you cannot add a double directly to that list.

3- P:S:your code here :

int choice = 1;
if (choice != 1)
{
    end=false;
}

will cause an endless while loop in your main method.

Hope this helps.

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

12 Comments

Ah yes, dur, I am supposed to be matching to the name arraylist not sending back the numbers. I'll fix that first.
I can't use next() for the iterator either because it won't let be assign that to a double variable...
Use a typecast then.. You can type cast whatever comes out of iter.next() to a double. double var = (double)iter.next(); Although, if you have a Double ArrayList, you should be able to directly assign it to a double variable only if you are using a double Iterator like : Iterator < Double > ;
Ok cool that worked. Now the errors just seem to not be recognizing my arraylists from the main method. Can my other method not see what's inside those even when they are passed in as parameters? It's saying can't find object when looking for the arraylists.
Okay, you do not need the for loop inside the iterator again right? Also, instead of customers(i) use customers.get(i). Please do make sure to mark the answer as correct, so that everyone knows that your question(s) are answered
|

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.