1

Trying to run the below program but keep getting error. I am not sure where I went wrong. Any assistance would be greatly appreciated.

run:

Welcome to Mike and Diane's Pizza

Enter your first name: Mike

Pizza Size (inches ) Cost:

10 $10.99

12 $12.99

14 $14.99

16 $16.99

What size pizza would you like?

10, 12, 14, or 16 (enter the number only): 12

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code

Java Result: 1 BUILD SUCCESSFUL (total time: 22 seconds)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
// You have to add an import statement to use the DecimalFormat class
import java.text.DecimalFormat;

public class Pizza_Order {
    public static void main(String[] args) {
        //TASK #5 Create a DecimalFormat object with 2 decimal places
        // You have to add code!!!
        DecimalFormat DollarFormat = new DecimalFormat();
        //Create a Scanner object to read input
        Scanner keyboard = new Scanner(System.in);

        //Create an instance of a Pizza
        Pizza order = new Pizza();

        String firstName;           //user's first name

        boolean discount = false;       //flag,

        //true if user is eligible for discount

        int inches;             //size of the pizza

        char crustType;             //type of crust

        double cost;                //cost of the pizza

        final double TAX_RATE = .08;        //sales tax rate

        double tax;             //amount of tax

        char choice;                //user's choice

        String input;               //user input

        String toppings = "Cheese ";        //list of toppings

        int numberOfToppings = 0;       //number of toppings

        //prompt user and get first name

        System.out.println("Welcome to Mike and Diane's Pizza");

        System.out.print("Enter your first name:  ");

        firstName = keyboard.nextLine();

        //determine if user is eligible for discount by

        //having the same first name as one of the owners

        //TASK #1

        // You have to add code!!!

        if(firstName == "Mike" || firstName == "mike")

        {

            discount = true;
        }

        if(firstName == "Diane" || firstName == "diane")

        {

            discount = true;
        }

        //prompt user and get pizza size choice

        System.out.println("Pizza Size (inches ) Cost");

        System.out.println("                10            $10.99");

        System.out.println("                12            $12.99");

        System.out.println("                14            $14.99");

        System.out.println("                16            $16.99");

        System.out.println("What size pizza would you like?");

        System.out.print("10, 12, 14, or 16 (enter the number only): ");

        inches = keyboard.nextInt();

        //set price and size of pizza ordered

        //ADD LINES HERE FOR TASK #2

        // You have to add code!!!

        if(inches == 10)

        {

            order.setSize(10);

            order.setCost(10.99);
        } else if(inches == 12)

        {

            order.setSize(12);

            order.setCost(12.99);
        } else if(inches == 14)

        {

            order.setSize(14);

            order.setCost(14.99);
        } else if(inches == 16)

        {

            order.setSize(16);

            order.setCost(16.99);
        } else

        {

            order.setSize(12);

            order.setCost(12.99);

            System.out.println("A size other than the available"

                               + " sizes was select, a 12 inche pizza will be made.");
        }

        //consume the remaining newline character

        keyboard.nextLine();

        //prompt user and get crust choice

        System.out.println("What type of crust do you want? ");

        System.out.println("(H)Hand-tossed, (T) Thin-crust, or "

                           + "(D) Deep-dish (enter H, T, or D:): ");

        input = keyboard.nextLine();

        crustType = input.charAt(0);

        //set user's crust choice on pizza ordered

        //ADD LINES FOR TASK #3

        // You have to add code!!!

        switch(crustType)

        {

            case 'H':

            case 'h':

                order.setCrust("Hand-tossed");

                break;

            case 'T':

            case 't':

                order.setCrust("Thin-crust");

                break;

            case 'D':

            case 'd':

                order.setCrust("Deep-dish");

                break;

            default:

                System.out.println("A choice other than the available choices was made,"

                                   + " a hand-tossed pizza will be made.");

                order.setCrust("Hand-tossed");
        }

        //prompt user and get topping choices one at a time

        System.out.println("All pizzas come with cheese.");

        System.out.println("Additional toppings are $1.25 each,"

                           + " choose from");

        System.out.println("Pepperoni, Sausage, Onion, Mushroom");

        //if topping is desired,

        //add to topping list and number of toppings

        System.out.print("Do you want Pepperoni? (Y/N): ");

        input = keyboard.nextLine();

        choice = input.charAt(0);

        if(choice == 'Y' || choice == 'y')

        {

            numberOfToppings += 1;

            toppings = toppings + "Pepperoni ";
        }

        System.out.print("Do you want Sausage? (Y/N): ");

        input = keyboard.nextLine();

        choice = input.charAt(0);

        if(choice == 'Y' || choice == 'y')

        {

            numberOfToppings += 1;

            toppings = toppings + "Sausage ";
        }

        System.out.print("Do you want Onion? (Y/N): ");

        input = keyboard.nextLine();

        choice = input.charAt(0);

        if(choice == 'Y' || choice == 'y')

        {

            numberOfToppings += 1;

            toppings = toppings + "Onion ";
        }

        System.out.print("Do you want Mushroom? (Y/N): ");

        input = keyboard.nextLine();

        choice = input.charAt(0);

        if(choice == 'Y' || choice == 'y')

        {

            numberOfToppings += 1;

            toppings = toppings + "Mushroom ";
        }

        //set number of toppings and topping list on pizza ordered

        order.setNumToppings(numberOfToppings);

        order.setToppingList(toppings);

        //add additional toppings cost to cost of pizza

        order.setCost(1.25 * numberOfToppings);

        //display order confirmation

        System.out.println();

        System.out.println("Your order is as follows: ");

        System.out.println(order.getSize() + " inch pizza");

        System.out.println(order.getCrust() + " crust");

        System.out.println(order.getToppingList());

        //display cost of pizza

        cost = order.getCost();

        //apply discount if user is elibible

        //ADD LINES FOR TASK #4 HERE

        // You have to add code!!!

        if(discount = true)

        {

            System.out.println("You are eligible for a $2.00 discount!!");

            order.setCost(cost - 2);
        }

        //EDIT PROGRAM FOR TASK #5

        //SO ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES

        System.out.println("The cost of your order is: $" + DollarFormat.format(cost));

        //calculate and display tax and total cost

        tax = cost * TAX_RATE;

        System.out.println("The tax is:  $" + DollarFormat.format(tax));

        System.out.println("The total due is:  $" + DollarFormat.format(tax + cost));

        System.out.println("Your order will be ready" +

                           " for pickup in 30 minutes.");
    }
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • 1
    Please format your code. – Sotirios Delimanolis Feb 28 '14 at 00:44
  • 4
    And eliminate line numbers. – aliteralmind Feb 28 '14 at 00:44
  • 1
    This isn't related to your current problem but you should read [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – PakkuDon Feb 28 '14 at 00:46
  • possible duplicate of [java.lang.RuntimeException: Uncompilable source code - what can cause this?](http://stackoverflow.com/questions/2333285/java-lang-runtimeexception-uncompilable-source-code-what-can-cause-this) – Sotirios Delimanolis Feb 28 '14 at 00:47
  • How are you running this program? Any chance you are using eclipse or any other IDE, and that your code doesn't compile? Then you will get this error when that code is reached – xp500 Feb 28 '14 at 00:48
  • Please bear with me I am in the process of deleting the numbered lines. – PowerofMerlin Feb 28 '14 at 00:51
  • We may need to see the `Pizza` class. – ajb Feb 28 '14 at 00:51
  • 1
    I'm not seeing anything in the code itself that would be causing this exception. Delete your .class file and try recompiling. (By the way - I'm tutoring a student in your class with this exact same homework assignment. I wish you the best of luck. :) ) – Makoto Feb 28 '14 at 00:56
  • Umm well I can't seem to find anything that would be causing this either and it is very confusing. I have tried almost all the suggestions, but no luck yet. But thank you everyone for responding. – PowerofMerlin Feb 28 '14 at 00:59
  • There could be errors if `order.setSize` and `order.setCost` aren't being called correctly. I think we need to see at least the declarations of how the `Pizza` methods are declared. Does `order.setCost` take a `float` parameter? – ajb Feb 28 '14 at 01:02
  • @ajb my `Pizza` class does take a `float` parameter. – PowerofMerlin Feb 28 '14 at 01:06

1 Answers1

1
class Pizza {
    public void setCost(float f) { ... }
    // other stuff
}

order.setCost(12.99);

This won't compile, because 12.99 is a double by default, and the compiler doesn't like it when you try to automatically convert a double to a float. Either change the parameter of setCost to double, or put an F after all the literals that need to be a float, e.g.

order.setCost(12.99F);

[You really shouldn't be using either one to represent monetary values, but rather BigDecimal instead, since float and double won't represent decimal places exactly. However, if you're using double, you probably have to be working with numbers in the billions of dollars before it makes a difference. Put that on the list for something to learn later.]

ajb
  • 31,309
  • 3
  • 58
  • 84
  • 1
    Ohhhh! My mind just has been blown. Thank you this helped me soo much! – PowerofMerlin Feb 28 '14 at 01:13
  • Actually, you don't need BigDecimal either for most simple financial programming. Represent your cash as pennies rather than fractional dollars, and you can use integers -- which don't have any of the complications or costs of either floating point or BigDecimal. "Fixed-point numbers", meaning ints reformatted only for printing, are simpler, faster and cheaper than either of the alternatives. – keshlam Feb 28 '14 at 03:13
  • @keshlam Right, you just have to remember to scale it yourself. The Ada language has fixed-point numbers built-in, where numbers are represented as integers but the compiler keeps track of the scale factor. It shouldn't be hard to devise a class to do the same thing in Java, if this hasn't been done already. – ajb Feb 28 '14 at 16:24