I just started to learn java, and the first program I have to create is called PizzaRun, here is description:

PizzaRun is a simple program that calculates the number of pizzas to buy based on the sum of the slices entered on the command line arguments after the first argument, the price per pizza.

The example below shows an execution of the program on the command line (the dollar sign is the terminal window's prompt, and the # starts a terminal 'shell comment' to the end of the line).

Example: An 8-slice pizza costs $9.75 and four people want 1, 3, 4 and 2 slices.

$ java PizzaRun 9.75 1 3 4 2
# produces this output:
Buy 2 pizzas for $19.5
There will be 6 extra slices.

What I have so far is this:

import java.util.Scanner;
import java.util.ArrayList;

class PizzaRun {
    public static void main(java.lang.String[] args){

        System.out.println("buy" + calcWholePies(9) + "pizzas");
    public static int slicePerPie = 8;

    public static int calcWholePies(int nSlices)
        int pies = 0;
        while (nSlices > 0){
            if (nSlices > slicePerPie){
                nSlices = nSlices - slicePerPie;
        return pies;


I'm entering 9 into my calc method just for testing, but it doesn't seem to work when I run it through the terminal since nothing happens. I think it's getting stuck in an infinite loop or something.

Any tips? Is there a better way to calculate it other than a loop?

Thank you.

Edited by Eccoo

3 Years
Discussion Span
Last Post by Eccoo

To debug a problem like this, put a temporary print startement inside your loop (eg print the value of nSlices). Then you will be a ble to see if it is looping, and ifits an infinite loop you will get clues as to why that is.
And yes, you will need a loop, but not necessarily for this part, which you can do using division and remainder.

Edited by JamesCherrill


Last time I wrote code was in python 5 months ago, can't believe I forgot to include debugging print statements haha. Thanks bro, made me see the mistake and fixed it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.