Variables and parameters with methods

I am doing a practice exercise because I really need to improve my java rapidly. The application is for a pizza ordering program and I need to keep most of the user input in variables until the end but they are returning as null and 0. I am clearly doing something wrong can someone enlighten me I learn best from my own code and reading others examples can be confusing for me.I need the variables from the methods that get the pizza size, pizza crust and toppings etc to display in the receipt method

import java.util.Scanner;


public class test {
    ;
    public static final double TEN_INCH_PRICE=10.99;
    public static final double TWELVE_INCH_PRICE=12.99;
    public static final double FOURTEEN_INCH_PRICE=14.99;
    public static final double SIXTEEN_INCH_PRICE=16.99;
    public static final double TOPPING_COST=1.25;

    public static void main(String[] args) {
        int pizzaChoice = 0;
        String pizzaSize = null;
        int crustChoice;
        int toppingChoice;
        String crustWanted = null;
        String toppingWanted = null;
        String customerName;
        double pizzaPrice = 0;

        displayWelcome();
        pizzaSize(pizzaSize);
        pizzaCrust(crustWanted);
        toppingChoice(toppingWanted);
        pizzaCost(pizzaChoice, pizzaPrice);
        receipt(pizzaSize, crustWanted, toppingWanted,pizzaPrice);
    }



    /** 
     * Display Welcome Message
     * */
    public static void displayWelcome() {

        System.out.println("******************************************");
        System.out.println("* CIT Pizza *");
        System.out.println("******************************************");

    }


    public static String getname() {



        return null;

    }

    public static String pizzaSize(String pizzaSize){
        System.out.println("Pizza Size (inches) Cost");
        System.out.println("=================== ====");
        System.out.println("10                  10.99");
        System.out.println("12                  12.99");
        System.out.println("14                  14.99");
        System.out.println("16                  16.99 ");
        String message = "Please enter the size of pizza you want";
        int pizzaChoice=readValidPizzaSize(message); //Get pizza size choice
        if (pizzaChoice==10)
            pizzaSize="10 inch pizza";

        if (pizzaChoice==12)
            pizzaSize="12 inch pizza";

        if (pizzaChoice==14)
            pizzaSize="14 inch pizza ";

        if (pizzaChoice==16)
            pizzaSize="16 inch pizza";


        return pizzaSize;

    }

    public static String pizzaCrust(String crustWanted){
        System.out.println("Pizza Crust Choice");
        System.out.println("==================");
        System.out.println("All crusts cost the same price.");
        System.out.println("1 - Hand-tossed");
        System.out.println("2 - Thin-crust");
        System.out.println("3 - Deep-dish"); 
        String message = ("Please enter crust choice");
        int crustChoice=readValidCrustChoice(message);
        if (crustChoice==1)
            crustWanted="Hand-Tossed";

        if (crustChoice==2)
            crustWanted="Thin-crust";

        if (crustChoice==3)
            crustWanted="Deep-dish";


        return crustWanted;

    }

    public static String toppingChoice(String toppingWanted){
        System.out.println("Pizza Topping Choice");
        System.out.println("====================");
        System.out.println("All pizzas come with cheese and tomato sauce.");
        System.out.println("An additional topping costs €1.25.");
        System.out.println("Choose from:");
        System.out.println("1 - Pepperoni");
        System.out.println("2 - Sausage");
        System.out.println("3 - Onion");
        System.out.println("4 - None"); 
        String message = ("Please enter topping choice");
        int toppingChoice=readValidToppingChoice(message);
        if (toppingChoice==1)
            toppingWanted="Pepperoni";

        if (toppingChoice==2)
            toppingWanted="Sausage";

        if (toppingChoice==3)
            toppingWanted="Onion";

        if (toppingChoice==4)
            toppingWanted="None";




        return toppingWanted;
    }

    public static double pizzaCost(int pizzaChoice, double pizzaPrice){
        if (pizzaChoice==10)
            pizzaPrice=TEN_INCH_PRICE;

        if (pizzaChoice==12)
            pizzaPrice=TWELVE_INCH_PRICE;

        if (pizzaChoice==14)
            pizzaPrice=FOURTEEN_INCH_PRICE;

        if (pizzaChoice==16)
            pizzaPrice=SIXTEEN_INCH_PRICE;


        return pizzaPrice;

    }

    public static void receipt(String pizzaSize, String crustWanted, String toppingWanted,double pizzaPrice){
        System.out.println("Order Details");
        System.out.println("============="); 
        System.out.println("your order is as follows:");
        System.out.println(pizzaSize);
        System.out.println(crustWanted);
        System.out.println("Cheese,Tomato sauce,"+ " "+toppingWanted);
        System.out.println("The cost of your order is:"+ " "+pizzaPrice);

    }
    public static int readInt(String prompt)
    {
        // Create a scanner object for input from the console window
        Scanner keyboard = new Scanner(System.in);

        // boolean flag which is used to control the
        // data validation loop

        boolean numGood = false; // Assume the worst   
        do
        {
            System.out.print(prompt);  // ask for the value
            if(!keyboard.hasNextInt()) // check if what's in the keyboard buffer is not a double
            {
                System.out.println("You must enter an  value!"); // display an error message
                keyboard.nextLine(); // consume the bad value entered
            }
            else
                numGood = true; // value entered is good
        } while(!numGood);
        // at this point we know the value in the
        // keyboard buffer is numeric so we can go ahead and
        // return it.
        return keyboard.nextInt();
    }
    /**
     * The readValidPizzaSize method reads an applicable pizza size
     * from the console window and will display an appropriate
     * error message if a non-applicable int value is entered.
     * @param prompt A prompt to request the user to enter a pizza size
     * @return The applicable int value entered.
     */
    public static int readValidPizzaSize(String prompt)
    {
        int value;
        do
        {
            value = readInt(prompt); // ask for and read an double value
            if (value !=10 && value !=12 && value!=14 && value!=16) // check if the value entered is applicable
                // display an error message
                System.out.println("Error - you must enter a valid pizza size");
        } while (value !=10 && value !=12 && value!=14 && value!=16);
        // at this point we know the value entered is positive
        // so return it
        return value;
    }

    public static int readValidCrustChoice(String prompt)
    {
        int value;
        do
        {
            value = readInt(prompt); // ask for and read an double value
            if (value !=1 && value !=2 && value!=3) // check if the value entered is applicable
                // display an error message
                System.out.println("Error - you must enter a valid crust choice");
        } while (value !=1 && value !=2 && value!=3);
        // at this point we know the value entered is positive
        // so return it
        return value;
    }
    public static int readValidToppingChoice(String prompt)
    {
        int value;
        do
        {
            value = readInt(prompt); // ask for and read an double value
            if (value !=1 && value !=2 && value!=3 &&value!=4) // check if the value entered is applicable
                // display an error message
                System.out.println("Error - you must enter a valid topping choice");
        } while (value !=1 && value !=2 && value!=3 && value!=4);
        // at this point we know the value entered is positive
        // so return it
        return value;
    }
}

Answers


Just change your main method to:

public static void main(String[] args) {
    int pizzaChoice = 0;
    String pizzaSize = null;
    int crustChoice;
    int toppingChoice;
    String crustWanted = null;
    String toppingWanted = null;
    String customerName;
    double pizzaPrice = 0;

    displayWelcome();
    pizzaSize = pizzaSize(pizzaSize);
    pizzaChoice = Integer.parseInt(pizzaSize.replaceAll("[^0-9]", ""));
    crustWanted = pizzaCrust(crustWanted);
    toppingWanted = toppingChoice(toppingWanted);
    pizzaPrice = pizzaCost(pizzaChoice, pizzaPrice);
    receipt(pizzaSize, crustWanted, toppingWanted,pizzaPrice);
}

The problem is that whenever you assign a new text to a String, you're actually creating a new String object in Java. In addition, Java's pass-by-reference, passes a copy of the reference. As a result, you're assigning a new object to a copy of the reference; hence, your original String is not affected. You might wanna study how objects and primitive types are passed in Java.


Need Your Help