java - 无法引用变量 - 找不到符号
问题描述
我正在制作我创建的“项目”类型的 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");
}
}
}
解决方案
您的代码有很多需要工作的地方,但正如评论中所建议的,主要问题是您的类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))
推荐阅读
- python-3.x - AttributeError:“列表”对象没有属性“特征”
- java - 用Java编写程序,通过数组和乘法后弹出元素
- c# - 如何使用 Speechsynthezier 在设备上设置和发送语言?(Windows.Media.Speechsynthesis)
- ruby-on-rails - 如何从 ruby on rails 中的元标记中获取内容值?
- linkedin - 通过 LinkedIn API V2 创建图像共享不起作用
- android - Android 项目:这项工作被卡住了,因为该项目没有在线分配任何跑步者。进入跑步者页面
- pyqt5 - 运行pyqt5程序,报错:此应用程序无法启动,因为它无法在“”中找到或加载Qt平台插件“xcb”
- python - 将 tensorflow 运算符分别应用于批处理中的每个数据,但需要并行运行它们
- javascript - Django admin中Select2 4.0.5常见错误
- javascript - 在jquery中添加'/'和第二个字符