CarrotsForHire
CarrotsForHire

Reputation: 69

Cannot reference a variable - cannot find symbol

I'm making an ArrayList of a type 'Item' that I've created. The class has a variable called name to describe the item. The items are stored in an ArrayList.

I cannot figure out how to reference the name variable because it is producing an error in the compiler that says cannot find symbol - variable name. Specifically, I reference it like this:

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

I've also tried referencing it like this:

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

It produces the same error. Please help! Here is the relevant code:

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");
        }
    }
}

Upvotes: 1

Views: 523

Answers (1)

sorifiend
sorifiend

Reputation: 6307

There is a lot going on with your code that needs work, but as suggested in the comments the main issue is that the variable name for example doesn't exist in your Item class, in fact no variables exist in your Item class.

To solve this you need to create class variables that you can reference from elsewhere, note how there are defined directly in the Item class not in the Item(...) method:

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

Now you can create an Item and get the values like normal:

//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);

And you will get the following output:

ID: 1
Name: Apple Fritter
Price: 1.49

Note as per the comments to your question you need to use the String.equals(...) method to compare strings, you can't compare strings using ==. For example this is the correct way to compare the name string to the keyword string: if(temp.name.equals(keyword))

Upvotes: 2

Related Questions