error: cannot find symbol when declaring a new LunchBag object in my TestLunchBag class within the main method. There's also a separate class Lunch (with just an int field and constructors/getters/setters).
LunchBag<Lunch> lunchBox = new LunchBag<>();
The above declaration gives me an error of "error: cannot find symbol" referring to LunchBag despite LunchBag being defined as a class that implements a GeneralBag interface. Here's the specific error:
error: cannot find symbol
LunchBag<Lunch> lunchBox = new LunchBag<>();
symbol: class LunchBag
location: class TestLunchBag
The above block is repeated twice for the same line saying there are two errors.
I defined a Lunch class which just has an int field for a code that represents a lunch item (1 for apple, 2 for sandwich etc.). In my class definition of LunchBag
package arraybags;
import java.util.Arrays;
public class LunchBag<T> implements GeneralBagInterface<T> {
private T[] bag;
private static final int DEFAULT_CAPACITY = 10;
private int numberOfEntries;
/**
* Creates an empty bag whose initial capacity is 10.
*/
public LunchBag() {
this(DEFAULT_CAPACITY);
}
/**
* Creates an empty bag having a given initial capacity.
*
* @param capacity the integer capacity desired
*/
public LunchBag(int capacity) {
numberOfEntries = 0;
bag = (T[]) new Object[capacity];
}}
My TestLunchBag class' main method and the line where the error shows up is as written above:
LunchBag<Lunch> lunchBox = new LunchBag<>();
TestLunchBag class code:
package arraybags;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TestLunchBag{
public static void main(String[] args)
{
LunchBag<Lunch> lunchBox = new LunchBag<>();
Scanner in = new Scanner(System.in);
choice = in.nextInt();
System.out.println("Enter lunch item (ID):");
int userLunchItemEntry = in.nextInt();
Lunch lunch = new Lunch(userLunchItemEntry);
if (lunch.isValidValue == true)
{
lunchBox.add(lunch);
}
}}
File directory wise: I have a package called arraybags which has:
GeneralBagInterface.java
Lunch.java
LunchBag.java
TestLunchBag.java
my interface's import and def statement:
package arraybags;
public interface GeneralBagInterface <T>{
//general bag operation methods that I override in my LunchBag class
}
Just if needed, Lunch class' definition dec and the packages:
package arraybags;
public class Lunch {
private int lunchItem;
public boolean isValid = false;
//and relevant methods
}
Am I declaring something wrong or could my default constructor not be valid/properly written? If so, how would I properly declare a new LunchBag object that takes in Lunch objects into it's bag array field?
LunchBag
是否LunchBag
编译并已导入到您的testLunchBag
类中(如果它们不在同一个包中)。
您的LunchBag
类定义似乎缺少结束的“}”。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.