【问题标题】:Getting cannot find symbol errors. ArrayLists, Iterator获取找不到符号错误。数组列表,迭代器
【发布时间】:2014-02-06 06:26:58
【问题描述】:

超市想要奖励当天的top顾客,即销售额最大的topN顾客,topN是程序的用户提供的一个值,在超市的屏幕上显示顾客的名字。为此,客户的购买金额存储在 ArrayList 中

实现一个方法:public static ArrayList

编写一个程序,提示收银员输入所有价格和名称,将它们添加到两个数组列表中,调用您实现的方法,并显示结果。使用 0 的价格作为标记。

我在编译时的错误是:

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

这是我所拥有的:

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

【问题讨论】:

    标签: java arraylist iterator


    【解决方案1】:

    1- 您收到错误是因为 Iterator 类没有名为 nextDouble() 的方法。 请查看Iterator API 以获取支持的方法列表。

    2- topCust 是一个 String ArrayList,您不能直接向该列表添加双精度。

    3- P:S: 你的代码在这里:

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

    会在你的 main 方法中造成一个无限循环。

    希望这会有所帮助。

    【讨论】:

    • 啊,是的,dur,我应该匹配名称 arraylist 而不是发回数字。我会先解决这个问题。
    • 我也不能将 next() 用于迭代器,因为它不会将其分配给双变量...
    • 然后使用类型转换。您可以将 iter.next() 中的任何内容类型转换为双精度。双变量=(双)iter.next();虽然,如果你有一个 Double ArrayList,你应该能够直接将它分配给一个 double 变量,只有当你使用像这样的双迭代器时: Iterator ;
    • 好吧,很酷。现在错误似乎无法从主要方法中识别出我的数组列表。即使将它们作为参数传入,我的其他方法是否也看不到其中的内容?就是说在查找数组列表时找不到对象。
    • 好吧,你不需要再迭代器内部的for循环了吧?此外,使用customers.get(i) 代替customers(i)。请务必将答案标记为正确,以便每个人都知道您的问题已得到解答
    猜你喜欢
    • 2013-11-18
    • 2016-09-10
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    相关资源
    最近更新 更多