Hi,
I've been working on this for two days. I need to pass an arraylist to another class. It gets the information, and will print out the menu, until the program goes back to the initial class, then the list empties. I don't get it, and I'm so confused.

public class Party
{
    private String inputLine;
    public Party()
    {
        reader = new Scanner(System.in);

    }
    public void start()
    {
        System.out.println("welcome. please enter food.");
        enterfood();
    }

    public void enterfood() {
        Food foodObject = new Food();
        foodObject.printA();
        printMenu();

           }
     
    public void printMenu()
    {
        System.out.println("Hello your menu will be here eventually.");
        Food foodObject = new Food();
        foodObject.printC();
        
    }

Here is the second class

public class Food
{
    /**
     *instance variables
     *///
    private Scanner reader;
    ArrayList<String> eat = new ArrayList<String>();
    
    
    /**
     * Constructor for objects of class Calculator
     * Create the ArrayList object.
     * Create the Scanner input object.
     */
   
    public Food()
    {
        reader = new Scanner(System.in);
        eat = new ArrayList<String>();

    }
    public void foodList()
    {
        
        System.out.println("Choose an action: (A) add food or (C) print list.");
        String inputLine = reader.nextLine();
       if(inputLine.equalsIgnoreCase("A")){
           printA();
        }
        if(inputLine.equalsIgnoreCase("C")){
            printC();
        }
            
            }
    public void printA()
    {
        Scanner reader = new Scanner(System.in);
        System.out.println("enter food: ");
        String n = reader.nextLine();
        eat.add(n);

        foodList();
    }
    public void printC()
    {
        System.out.println(eat);
    }

   
   public void next()
   {
       System.out.println("To enter more food type F.");
       String str= reader.nextLine();
       if(str.equalsIgnoreCase("F"))
       {
           foodList();
        }
       
    }
       
   
   public void eat(){
       
       System.out.println("Menu: " + eat);
    }

This is my output:
welcome. please enter food.
enter food:
pizza
Choose an action: (A) add food or (C) print list.
a
enter food:
soda
Choose an action: (A) add food or (C) print list.
c
[pizza, soda]
Hello your menu will be here eventually.
[]See, the menu empties. This is supposed to be pizza, soda like above.

Any help would be greatly appreciated. Thank you!

you should declare foodObject as an instance variable in Party, and instantiate it in the constructor. Then it won't empty.

public class Party
{
    private String inputLine;
    private Food foodObject;
    public Party()
    {
        foodObject = new Food();
        reader = new Scanner(System.in);

    }

and then take out the foodObject = new Food(); in the methods printMenu enterFood

Edited 6 Years Ago by Gerbiler: n/a

Thank you so much Gerbiler! And to think that was two days of frustration, resolved in two seconds. Is there any chance of making my list prettier, i.e. without the [] around my list?

I will mark this solved!

Again, I can't thank you enough!

well, that is a bit harder, instead of doing System.out.print(eat); do this

for(int i = 0;i<eat.length();i++){
   System.out.print(eat.get(i)+" ");
}

Is there any chance of making my list prettier, i.e. without the [] around my list?

Just change the printC() method to print it in the way that you want it. To get access to the Strings in eat you could either use the traditional for-loop like:

for(int i = 0; i < eat.size(); i++){
String s = eat.get(i)
//Do stuff with s to make it look like you want it
}

or do it in a cleaner for-each like:

for(String s : eat){
//Do stuff here...
}

Edit: Gerbiler beat me to it :)

Edited 6 Years Ago by Slimmy: n/a

It says it can't find method length? I did actually have all that above, I changed it after I posted, but where you have .length I had .size

When I changed it to length, I got the error message.
Any idea why?

It says it can't find method length? I did actually have all that above, I changed it after I posted, but where you have .length I had .size

When I changed it to length, I got the error message.
Any idea why?

Because length is not an attribute nor a method in ArrayList

Thank you, all! My menu still comes out [pizza, soda], but I can live with it!!

This question has already been answered. Start a new discussion instead.