找不到符号方法printinfo?

时间:2019-04-16 16:10:35

标签: java arraylist methods

我无法打印出糖果条的信息。每当我尝试使用该方法时,都会收到错误消息:cannot find symbol-method printINFO

我尝试使用n.printInfo()system.out.println(n.printinfo()),但仍然无法正常工作。我该怎么办?

方法类:

public class CandyBar
{
    public enum Color
    {
        BLUE, BROWN, GOLD, GREEN, ORANGE,
        PURPLE, RED, SILVER, WHITE, YELLOW
    }

    // instance variables
    private String name;
    private double weight;
    private Color wrapper;

    /**
     * Constructor for objects of class CandyBar
     */
    public CandyBar(String n, Color c, double w)
    {
        this.name = n;
        this.weight = w;
        this.wrapper = c;
    }

    public String getName()
    {
        return this.name;
    }

    public double getWeight()
    {
        return this.weight;
    }

    public Color getWrapperColor()
    {
        return this.wrapper;
    }

    public void printInfo()
    {
        System.out.println(this.name);
        System.out.printf("%.1f oz with a ", this.weight);
        System.out.println(this.wrapper + " wrapper");
    }
}

主类:

import java.util.ArrayList;

public class Lab16
{
    public static void main(String[] args)
    {
        ArrayList<CandyBar> bars = new ArrayList<CandyBar>();
        addCandyBarsToList(bars);

        /**
         * Use the methods you wrote below to answer all of
         * the following questions.
         */

        /**
         * Part A:
         */

        /**
         * Is Twix in the list?
         * If so, print all information about it.
         * If not, print a message to the user.
         */

        /**
         * Is Mounds in the list?
         * If so, print all information about it.
         * If not, print a message to the user.
         */


        /**
         * Part B:
         */

        /**
         * Print all the names of candy bars with yellow wrappers.
         */


        /**
         * Part C:
         */

        /**
         * Count how many candy bars are 1.75 oz or more.
         */


        /**
         * Part D:
         */

        /**
         * Is there a candy bar that is 1.86 oz?
         * If so, print the information.
         * If not, print a message to the user.
         * Write a binary search method to get the answer.
         */
    }

    /**
     * This method searches a list to find a candy bar by name.
     *
     * @param   list    the list to search
     * @param   n       a name to find
     * @return          the index of the candy bar 
     */
    public static int findCandyBar(ArrayList<CandyBar> list, String n)
    {
        for(int i = 0; i < list.size(); i++){
            if(list.get(i).getName() == n){
                return n.printInfo(i);
            }else{
                System.out.println("The list doesnt have that Candy");
            }
        }
        return 1;
    }
    /**
     * This method prints the names of the candy bars that have a certain
     * wrapper color.
     *
     * @param   list    the list to search
     * @param   c       the color wrapper to find 
     */
    public static void findByColor(ArrayList<CandyBar> list, CandyBar.Color c)
    {

    }

    /**
     * This method counts the number of candy bars that weigh greater than
     * or equal to the weight input parameter.
     *
     * @param   list    the list to search
     * @param   w       the weight to compare to
     * @return          the count of candy bars 
     */
    public static int countByWeight(ArrayList<CandyBar> list, double weight)
    {
        return 0;
    }

    /**
     * This method searches a list using binary search.
     *
     * @param   list    the list to search
     * @param   first   the first index
     * @param   last    the last index
     * @param   w       the value to search for
     * @return          the index of the candy bar 
     */
    public static int binaryFind(ArrayList<CandyBar> list, int first, int last, double w)
    {
        return -1;
    }

    /**
     * This method adds candy bars to a list.
     *
     * @param   list    the list to add to
     */
    public static void addCandyBarsToList(ArrayList<CandyBar> list)
    {
        CandyBar kitkat = new CandyBar("KitKat", CandyBar.Color.RED, 1.5);
        list.add(kitkat);
        CandyBar grand = new CandyBar("One-hundred Grand", CandyBar.Color.RED, 1.5);
        list.add(grand);
        CandyBar crunch = new CandyBar("Crunch", CandyBar.Color.BLUE, 1.55);
        list.add(crunch);
        CandyBar hershey = new CandyBar("Hershey", CandyBar.Color.BROWN, 1.55);
        list.add(hershey);
        CandyBar krackel = new CandyBar("Krackel", CandyBar.Color.RED, 1.55);
        list.add(krackel);
        CandyBar caramello = new CandyBar("Caramello", CandyBar.Color.PURPLE, 1.6);
        list.add(caramello);
        CandyBar what = new CandyBar("Whatchamacallit", CandyBar.Color.YELLOW, 1.6);
        list.add(what);
        CandyBar almond = new CandyBar("Almond Joy", CandyBar.Color.BLUE, 1.61);
        list.add(almond);
        CandyBar goodbar = new CandyBar("Mr. Goodbar", CandyBar.Color.YELLOW, 1.75);
        list.add(goodbar);
        CandyBar twix = new CandyBar("Twix", CandyBar.Color.GOLD, 1.79);
        list.add(twix);
        CandyBar henry = new CandyBar("Oh Henry", CandyBar.Color.YELLOW, 1.8);
        list.add(henry);
        CandyBar milkyWay = new CandyBar("Milky Way", CandyBar.Color.GREEN, 1.84);
        list.add(milkyWay);
        CandyBar payDay = new CandyBar("PayDay", CandyBar.Color.ORANGE, 1.85);
        list.add(payDay);
        CandyBar snickers = new CandyBar("Snickers", CandyBar.Color.BLUE, 1.86);
        list.add(snickers);
        CandyBar butterfinger = new CandyBar("Butterfinger", CandyBar.Color.YELLOW, 1.9);
        list.add(butterfinger);
        CandyBar musketeers = new CandyBar("Three Musketeers", CandyBar.Color.SILVER, 1.92);
        list.add(musketeers);
        CandyBar reeses = new CandyBar("Reese's FastBreak", CandyBar.Color.ORANGE, 2);
        list.add(reeses);
        CandyBar babyRuth = new CandyBar("Baby Ruth", CandyBar.Color.SILVER, 2.1);
        list.add(babyRuth);
    }
}

1 个答案:

答案 0 :(得分:0)

printInfo()对象上声明了CandyBar方法,但是在这里您试图在printInfo()对象上调用StringString上不存在此方法。

/**
 * This method searches a list to find a candy bar by name.
 *
 * @param   list    the list to search
 * @param   n       a name to find
 * @return          the index of the candy bar 
 */
public static int findCandyBar(ArrayList<CandyBar> list, String n)
{
    for(int i = 0; i < list.size(); i++){
        if(list.get(i).getName() == n){
            return n.printInfo(i); <-- calling n.printInfo(i) will not work as printInfo() does not exist on String

您需要注意printInfo()的方法签名:

  • 它存在于CandyBar上,而不存在于String上。
  • 它不接受任何参数参数。
  • 这是一个无效方法,即不返回任何对象。

所以代替:

return n.printInfo(i);

使用此:

i.printInfo();