-1

I declared a variable and tried to assign it a value on a switch statement. When I try to print it, it says it hasn't been initialized. For context I'm trying to print some sort of receipt as an output. Here is the code:

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    
    int price;
    int quantity;
    int totalprice;

    System.out.println("--------------------------------------------");
    System.out.println("!Welcome to FoodRhino Services!");
    System.out.println("--------------------------------------------");
    System.out.println("Please enter your name:");
    String name = scan.next();
    System.out.println("Please enter your address:");
    String address = scan.next();
    System.out.println("Please enter your contact number:");
    int cntcnum = scan.nextInt();
    System.out.println("--------------------------------------------");
   
    System.out.println("--------------------------------------------");
    System.out.println("Welcome " + name + ", " + "please pick a restaurant");
    System.out.println("--------------------------------------------");
    System.out.println("(1) Burger King");
    System.out.println("(2) Mcdonalds");
    System.out.println("(3) Arbys");
    System.out.println("(4) Goldilocks");
    System.out.println("(5) Papa Johns");
    System.out.println("--------------------------------------------");
    System.out.println("Pick a number:");
    int restonum = scan.nextInt();
    System.out.println("--------------------------------------------");

    switch (restonum) {
        case 1:
            System.out.println("--------------------------------------------");
            System.out.println("Choose a product");
            System.out.println("--------------------------------------------");
            System.out.println("(BK01) 4 Cheese Whopper - $129");
            System.out.println("(BK02) Double Whopper with Cheese - $159");
            System.out.println("(BK03) Bacon Cheese Burger - $179");
            System.out.println("--------------------------------------------");
            System.out.print("Enter Product Code: ");
            String bkpc = scan.next();
            System.out.print("How many: ");
            quantity = scan.nextInt();
            System.out.println("--------------------------------------------");
             if(bkpc == "BK01")
                price = 129;
             if(bkpc == "BK02")
                price = 159;
             if(bkpc == "BK03") 
                price = 179;
            
            break;
        case 2:
            System.out.println("--------------------------------------------");
            System.out.println("Choose a product");
            System.out.println("--------------------------------------------");
            System.out.println("(MCD01) Big Mac - $190");
            System.out.println("(MCD02) Chicken Mcnuggets - $180");
            System.out.println("(MCD03) Filet-o-fish - $155");
            System.out.println("--------------------------------------------");
            System.out.print("Enter Product Code: ");
            String mcdpc = scan.next();
            System.out.print("How many: ");
            quantity = scan.nextInt();
            System.out.println("--------------------------------------------");
             if(mcdpc == "MCD01")
                price = 190;
             if(mcdpc == "MCD02")
                price = 180;    
             if(mcdpc == "MCD03") 
                price = 155;
            break;
        case 3:
            System.out.println("--------------------------------------------");
            System.out.println("Choose a product");
            System.out.println("--------------------------------------------");
            System.out.println("(ARB01) Smokehouse Brisket - $357");
            System.out.println("(ARB02) Greek Gyro - $430");
            System.out.println("(ARB03) Loaded Italian - $412");
            System.out.println("--------------------------------------------");
            System.out.print("Enter Product Code: ");
            String arbpc = scan.next();
            System.out.print("How many: ");
            quantity = scan.nextInt();
            System.out.println("--------------------------------------------");
             if(arbpc == "ARB01")
                price = 357;
             if(arbpc == "ARB02")
                price = 430;    
             if(arbpc == "ARB03") 
                price = 412;
            
            break;
        case 4:
            System.out.println("--------------------------------------------");
            System.out.println("Choose a product");
            System.out.println("--------------------------------------------");
            System.out.println("(GLDCK01) Large Chocolate Mousse - $99");
            System.out.println("(GLDCK02) Brazo De Mercedes - $102");
            System.out.println("(GLDCK03) Black Forest Cake - $285");
            System.out.println("--------------------------------------------");
            System.out.print("Enter Product Code: ");
            String gldckpc = scan.next();
            System.out.print("How many: ");
            quantity = scan.nextInt();
            System.out.println("--------------------------------------------");
             if(gldckpc == "GLDCK01") {
                price = 99;  }
             if(gldckpc == "GLDCK02") {
                price = 102;  }   
             if(gldckpc == "GLDCK03") {
                price = 285; }
            break;
        case 5:
            System.out.println("--------------------------------------------");
            System.out.println("Choose a product");
            System.out.println("--------------------------------------------");
            System.out.println("(PPJ01) Large Pepperoni Pizza - $75");
            System.out.println("(PPJ02) Smoked Hawaiian Pizza - $90");
            System.out.println("(PPJ03) Cheesy Crust Pizza - $65");
            System.out.println("--------------------------------------------");
            System.out.print("Enter Product Code: ");
            String ppjpc = scan.next();
            System.out.print("How many: ");
            quantity = scan.nextInt();
            System.out.println("--------------------------------------------");
             if(ppjpc == "PPJ01") {
                price = 75; }
             if(ppjpc == "PPJ02") {
                price = 90;}    
             if(ppjpc == "PPJ03") {
                price = 65; }
            
            break;
        default:
                break;
    }
   
    System.out.println(price);
    
}

Please forgive my messy code as I only started learning programming a few months ago. English wasn't my first language either and I apologize if I didn't state my problem clear enough. If theres also a better method to what im trying to do please do say.

2 Answers2

1

The fundamental issue is that you do not assign a value to price on every possible path through the code. It is possible to get to the end without price having any value.

For example, in case 1, what if bkpc is anything other than BK01, BK02, or BK03? "Can't happen", you say? Of course it can: it's user input, and you have no error checking. But even if it were not user input, you'd need error checking.

If those cases are supposed to not happen (as distinct from not being possible - they are possible) then the quickest fix might be to initialize `price' to something impossible, say negative 1, and then at the end:

if (price < 0) 
    System.out.println("Bad input"); // there may be better wording!
else
    System.out.println(price);

You also have a problem of string comparison, which needs to be fixed, but that does not change the fundamental problem of having no error handling.

something
  • 174
  • 4
  • I was also trying to figure that out but it wasn't my priority. Will try the solution you did thank you. – ItsuDemo May 07 '21 at 12:19
0

What happens when none of the switch cases are met? You'd be trying to print an uninitialized variable. That's not allowed, and the compiler is telling you that.

Simply initialize it to some default value:

int price = 0;
David
  • 208,112
  • 36
  • 198
  • 279
  • I did try to do that but it shows 0 instead of the value I wanted to print out which is the value that I assigned in the switch cases. – ItsuDemo May 07 '21 at 11:55
  • @ItsuDemo: It looks like there's a separate issue in the logic within those `switch` cases. `if(bkpc == "BK01")` is not how you [compare strings in Java](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – David May 07 '21 at 11:56
  • @ItsuDemo if you only want to print when you assign a value in a switch case, you have to put the print statement into each of the switch cases. Or throw in the default case. – Andy Turner May 07 '21 at 11:58
  • I see. Will try to use .equals() instead and see if it works. Thanks for the responses – ItsuDemo May 07 '21 at 11:58
  • @AndyTurner - it is sufficient to be able to **detect** that you have not assigned a price; see my answer. (And there's an argument to be made that it's simpler/easier to follow if you only print in one place, but that might come down to opnion) – something May 07 '21 at 13:18