首页 > 解决方案 > 无法引用变量 - 找不到符号

问题描述

我正在制作我创建的“项目”类型的 ArrayList。该类有一个name用于描述项目的变量。这些项目存储在 ArrayList 中。

我无法弄清楚如何引用该name变量,因为它在编译器中产生了一个错误,cannot find symbol - variable name.具体来说,我引用它是这样的:

for (int i=0; i < theMenu.size(); i++) {
                Item temp = theMenu.get(i);
                if(temp.name == keyword)
                    System.out.println("test");
            }

我也试过这样引用它:

for (int i=0; i < theMenu.size(); i++) {
                if(theMenu.get(i).name == keyword)
                    System.out.println("test");
            }

它产生相同的错误。请帮忙!以下是相关代码:

import java.util.ArrayList;
import java.util.Scanner;

public class Doms {

    public static class Item {
        public Item(int itemID, String itemName, double itemPrice, double itemSalePrice, String itemSaleBeginDate, String itemSaleEndDate, ArrayList<Integer> itemReviews, ArrayList<String> itemComments) {
            int id = itemID;
            String name = itemName;
            double price = itemPrice;
            double salePrice = itemSalePrice;
            String SaleBeginDate = itemSaleBeginDate;
            String SaleEndDate = itemSaleEndDate;
            ArrayList<Integer> reviews = itemReviews;
            ArrayList<String> comments = itemComments;
        }
    }

    public static void main(String[] args) {

        // A list containing each Item and all of its contents
        ArrayList<Item> items = new ArrayList<Item>();

        // Create Item elements
        ArrayList<Integer> reviews2 = new ArrayList<Integer>();
        ArrayList<String> comments2 = new ArrayList<String>();
        Item item2 = new Item(2, "Sour Cream Donut", 1.29, 1.29, "", "", reviews2, comments2);
        items.add(item2);

        //This part doesn't work
        for (int i=0; i < theMenu.size(); i++) {
            Item temp = theMenu.get(i);
            if(temp.name == keyword)
                System.out.println("test");
        }
    }
}

标签: javavariablesarraylisttypesreference

解决方案


您的代码有很多需要工作的地方,但正如评论中所建议的,主要问题是您的类name中不存在例如变量Item,实际上您的类中不存在变量Item

要解决这个问题,您需要创建可以从其他地方引用的类变量,注意如何直接在Item类中而不是在Item(...)方法中定义:

public static class Item {
    //Create class variables
    int id;
    String name;
    double price;
    double salePrice;
    String SaleBeginDate;
    String SaleEndDate;
    ArrayList<Integer> reviews;
    ArrayList<String> comments;

    public Item(int itemID, String itemName, double itemPrice, double itemSalePrice, String itemSaleBeginDate, String itemSaleEndDate, ArrayList<Integer> itemReviews, ArrayList<String> itemComments) {
        //Use the values passed into the method and store them as class variables
        id = itemID;
        name = itemName;
        price = itemPrice;
        salePrice = itemSalePrice;
        SaleBeginDate = itemSaleBeginDate;
        SaleEndDate = itemSaleEndDate;
        reviews = itemReviews;
        comments = itemComments;
    }
}

现在您可以像正常一样创建Item并获取值:

//Create an item
Item item1 = new Item(1, "Apple Fritter", 1.49, 1.29, "February 1, 2021", "July 1, 2021", reviews1, comments1);

//Get the public values from the item:
System.out.println("ID: " + item1.id);
System.out.println("Name: " + item1.name);
System.out.println("Price: " + item1.price);

您将获得以下输出:

ID: 1
Name: Apple Fritter
Price: 1.49

请注意,根据您对问题的评论,您需要使用该String.equals(...)方法来比较字符串,您不能使用==. 例如,这是将名称字符串与关键字字符串进行比较的正确方法:if(temp.name.equals(keyword))


推荐阅读