-4

The 2 answers posted both fixed it. thank you very much and i am embarrassed by how simple it was.

sorry for posting such a basic question and thank you for your time.

From the menu method at the top i wish to call 2 different methods to complete an action based on the input. i cannot find anywhere how to call a void method.

I don't know why the second one wont work as i used a similar thing earlier on.

public void CheckOutMenu(ArrayList basket )
{ 
    choice = 0;
    while (   choice !=4 ) {

        Scanner in = new Scanner ( System.in);

       {
            switch(in.nextInt()){
                //print the number of items in the basket
                case 1:
                //working
                break;

                case 2:
                      //dont know how to call on option 2 the listBasket method
                break;

                case 3:

                      //to call the method
                  //error and wont compile
                 double totalCost = CalcTotalCost(totalCost = 0);

                    //printing what the method returns
                System.out.print("The total price of your basket is £"  );

                break;

                case 4:
                choice = 4;
                break;

                default: 
                System.out.println("please enter a whole number that represents you're choice");

           }  

       }
    }   
}


protected void listBasket(ArrayList basket )
{
   //code inside works fine


}


public double CalcTotalCost(double total, ArrayList basket)
{ 
 //code inside works fine
 return total;
}
  • Two things: 1) Please format your code; it'll save you and others time. 2) What exactly is the error? Error messages always try to tell you what's wrong. – Dennis Meng Dec 16 '13 at 17:54

2 Answers2

0

you can call a void method like any other method, except that you don't have to do anything with the return value because there isn't one.

case 2:
    listBasket(basket);
    break;

for your third case, you have to pass in both a double and an ArrayList if you want to pass 0 as your double, than you can just post a double literal.

case 3:
    /* if you don't want totalCost to go out of scope right away, 
     * declare it above the switch statement */
    double totalCost = CalcTotalCost(0.0, basket);
    ...
    break;
0

Your error is here, as I'm sure you know:

double totalCost = CalcTotalCost(totalCost = 0);

However, your CalcTotalCost method is defined thus:

public double CalcTotalCost(double total, ArrayList basket)

You've sent it the total, but you haven't sent it a basket.

SimplyPanda
  • 705
  • 7
  • 16