0

I am attending college for computer programming and came across a practice question where I am keep getting a compiler error: local varialbe movieFee, snackFee and couponeFee is not initialized at line 85.

I am new to this and I feel like I have done everything right. Could someone kindly help me out?

Thank you! Joseph

Question is: Calculate the cost of going to the movie theatre. Pick the appropriate age range – child, adult, or senior; choose a snack bar combo – none, popcorn and pop, chocolate bar and pop, or popcorn and candy and pop; indicate if the patron has a coupon which gives him $2 off the movie fee.

Patron Age Ranges       Movie Fee       Snack Bar Options       Fee
child ( < 12)       $5.50       A - popcorn and pop     $7.90
adult (12 - 65)     $8.50       B - chocolate bar and pop       $6.30
senior ( > 65)      $6.00       C - popcorn and candy and pop       $8.90

My code is:

import java.util.Scanner; public class MovieFee {

public static void main(String[] args)
{
    //scanner class
    Scanner input=new Scanner(System.in);

    //display the age options and prompt the user to enter one
    System.out.println("Child (<12)");
    System.out.println("Adult (12-65)");
    System.out.println("Senior (>65)");
    System.out.print("Choose your age category: ");
    String age=input.nextLine();

    //display the snak bar options and prompt the user to enter one
    System.out.println("\n\nA - popcorn and pop");
    System.out.println("B - chocolate bar and pop");
    System.out.println("C - popcorn and candy and pop");
    System.out.print("What is your snack bar option(enter a letter): ");
    String snackOption=input.nextLine();

    //calculate the movie fee
    //declare fee variables
    double movieFee, snackFee;
    if(age=="child"||age=="Child")
    {
        movieFee=5.50;
    }
    else if(age=="adult"||age=="Adult")
    {
        movieFee=8.50;
    }
    else if(age=="senior"||age=="Senior")
    {
        movieFee=6.00;
    }
    else
    {
        System.out.println("\nYou did not enter a correct age group.");
        System.out.println("Please try again using one of: child, adult, senior");
    }

    //calculate the snack fee
    if(snackOption=="A"||snackOption=="a")
    {
        snackFee=7.90;
    }
    else if(snackOption=="B"||snackOption=="b")
    {
        snackFee=6.30;
    }
    else if(snackOption=="C"||snackOption=="c")
    {
        snackFee=8.90;
    }
    else
    {
        System.out.println("\nYou did not enter a correct option");
        System.out.println("Please try again using either A, B or C");
    }

    //ask the user if he/she has a coupon
    double couponFee;
    System.out.print("\nDo you have a coupon? Type yes or no: ");
    String coupon=input.nextLine();
    if(coupon=="yes")
    {
        couponFee=2.0;
    }
    else if(coupon=="no")
    {
        couponFee=0;
    }


    //calculate the total fee and total fee
    double result=movieFee+snackFee+couponFee;   //**GETTING ERROR HERE
    System.out.println("Final price is "+result);



}
//end main
}
//end class
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
Joseph
  • 13
  • 5

7 Answers7

2

Two type of problems with your code:

First problem

You have not initialized the method local variables. As per Java specs, local variables need to be initialised before use. So do something like :

    double movieFee = 0.0d;
    double snackFee = 0.0d;

Second problem

Strings should be compared using equals method and not using == . So u will have to change the this and other conditions

    if(age=="child"||age=="Child")

as

if(age.equals("child") || age.equals("Child"))
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • However, he wouldn't have to initialize them if he properly re-prompted the user for the 'else' case as the variables wouldn't be used until a valid input was received. Consider putting the prompts along with the value checking inside of a while loop. – LINEMAN78 Feb 10 '16 at 05:48
  • thank you so much my program now works :) – Joseph Feb 10 '16 at 18:19
  • @Joseph Glad to help.There is a better way to say thanks on stackoverflow by accepting the answer. You can accept the answer by clicking on the tick mark left to the most helpful answer. Accepting an answer helps other people facing the same problem. – Juned Ahsan Feb 10 '16 at 23:05
0

In some scenarios those variables will not be initialized at the line reported by the compiler because of the ifs conditions.

Pablo
  • 2,581
  • 3
  • 16
  • 31
  • Not sure why the downvote as this is actually the correct answer to the question as asked. Maybe the downvoter wanted to see more of an explanation with code? – Jim Garrison Feb 10 '16 at 05:35
0

Compare Strings using .equals().

E.g.:

if(snackOption.equals("A") || snackOption.equals("a")) {
    snackFee = 7.90;
}

Also, you could use equalsIgnoreCase().

E.g.:

if(snackOption.equalsIgnoreCase("A")) {
    snackFee = 7.90;
}

And finally, instead of:

else if(coupon=="no") {
    couponFee=0;
}

Write:

else {
    couponFee = 0.0;
}

Another tought: you are not checking the age in a while loop, so if an invalid age is entered, you just notify the user but you don't ask again.

Peter
  • 323
  • 1
  • 16
  • 46
0

You don't have a default catcher for your conditions in the below if else-if condition.

Compiler can't assume during compile time that the couponFee variable will have a default value at run-time or the it will hit either in if or else-if.

    double couponFee;
    System.out.print("\nDo you have a coupon? Type yes or no: ");
    String coupon=input.nextLine();
    if(coupon=="yes")
    {
        couponFee=2.0;
    }
    else if(coupon=="no")
    {
        couponFee=0;
    }

This means that you have to help the compiler by

  1. provide a default value for couponFee = 0;
  2. add else condition which will help the compiler know that the variable will not be left in uninitialized state;

But if i were you, i'll just have a default value for couponFee variable and make the code short.

    double couponFee = 0;
    System.out.print("\nDo you have a coupon? Type yes or no: ");
    if("yes".equals(input.nextLine()) couponFee=2.0;
Ruelos Joel
  • 2,209
  • 3
  • 19
  • 33
0
import java.util.Scanner;

public class MovieFee {

    public static void main(String[] args) {
        // scanner class
        Scanner input = new Scanner(System.in);
        // display the age options and prompt the user to enter one
        System.out.println("Child (<12)");
        System.out.println("Adult (12-65)");
        System.out.println("Senior (>65)");
        System.out.print("Choose your age category: ");
        String age = input.nextLine();

        // display the snak bar options and prompt the user to enter one
        System.out.println("\n\nA - popcorn and pop");
        System.out.println("B - chocolate bar and pop");
        System.out.println("C - popcorn and candy and pop");
        System.out.print("What is your snack bar option(enter a letter): ");
        String snackOption = input.nextLine();

        // calculate the movie fee
        // declare fee variables
        double movieFee = 0.0, snackFee = 0.0;
        //if (age == "child" || age == "Child") {
        if (age.equalsIgnoreCase("child")) {
            movieFee = 5.50;
        }
        //else if (age == "adult" || age == "Adult") {
        else if (age.equalsIgnoreCase("adult")) {
            movieFee = 8.50;
        }
        //else if (age == "senior" || age == "Senior") {
        else if (age.equalsIgnoreCase("senior")) {
            movieFee = 6.00;
        }
        else {
            System.out.println("\nYou did not enter a correct age group.");
            System.out.println("Please try again using one of: child, adult, senior");
        }

        // calculate the snack fee
        //if (snackOption == "A" || snackOption == "a") {
        if (snackOption.equalsIgnoreCase("a")) {
            snackFee = 7.90;
        }
        //else if (snackOption == "B" || snackOption == "b") {
        else if (snackOption.equalsIgnoreCase("b")) {
            snackFee = 6.30;
        }
        //else if (snackOption == "C" || snackOption == "c") {
        else if (snackOption.equalsIgnoreCase("c")) {
            snackFee = 8.90;
        }
        else {
            System.out.println("\nYou did not enter a correct option");
            System.out.println("Please try again using either A, B or C");
            // You need to send them back to the start, or exit the program.
        }

        // ask the user if he/she has a coupon
        double couponFee = 0.0;
        System.out.print("\nDo you have a coupon? Type yes or no: ");
        String coupon = input.nextLine();
        if (coupon == "yes") {
            couponFee = 2.0;
        }
        else if (coupon == "no") {
            couponFee = 0;
        }

        // calculate the total fee and total fee
        double result = movieFee + snackFee + couponFee; // **GETTING ERROR HERE
        System.out.println("Final price is " + result);

    }
    // end main
}
// end class
csteel
  • 428
  • 1
  • 3
  • 14
0
public static void main(String[] args)
{
    String age;
    String snackOption;
    String coupon;
    //scanner class
    Scanner input=new Scanner(System.in);

     //ask the user if he/she has a coupon
    double couponFee=0;
    System.out.print("\nDo you have a coupon? Type yes or no: ");
    coupon=input.nextLine();
    if(coupon.equals("yes"))
    {
        couponFee=2.0;
        System.out.println("your saved" +couponFee);
    }
    else if(coupon.equals("no"))
    {
        couponFee=0;
        System.out.println("your saved" +couponFee);
    }


    //display the age options and prompt the user to enter one
    System.out.println("Child (<12)");
    System.out.println("Adult (12-65)");
    System.out.println("Senior (>65)");
    System.out.print("Choose your age category: ");
    age=input.nextLine();
    System.out.println("============" +age);

    //calculate the movie fee
    //declare fee variables

    double movieFee=0, snackFee=0;
    if(age.equals("child")||age.equals("Child"))
    {
        movieFee=5.50;
        movieFee=movieFee-couponFee;
        System.out.println("your movie Fee is" +movieFee);
    }
    else if(age.equals("adult")||age.equals("Adult"))
    {
        movieFee=8.50;
        movieFee=movieFee-couponFee;
        System.out.println("your movie Fee is" +movieFee);
    }
    else if(age.equals("senior")||age.equals("Senior"))
    {
        movieFee=6.00;
        movieFee=movieFee-couponFee;
        System.out.println("your movie Fee is" +movieFee);
    }
    else
    {
        System.out.println("\nYou did not enter a correct age group.");
        System.out.println("Please try again using one of: child, adult, senior");
    }

    //display the snak bar options and prompt the user to enter one
    System.out.println("\n\nA - popcorn and pop");
    System.out.println("B - chocolate bar and pop");
    System.out.println("C - popcorn and candy and pop");
    System.out.print("What is your snack bar option(enter a letter): ");
    snackOption=input.nextLine();

    //calculate the snack fee
    if(snackOption.equals("A")||snackOption.equals("a"))
    {
        snackFee=7.90;
        System.out.println("your snack Fee is" +snackFee);
    }
    else if(snackOption.equals("B")||snackOption.equals("b"))
    {
        snackFee=6.30;
        System.out.println("your snack Fee is" +snackFee);
    }
    else if(snackOption.equals("C")||snackOption.equals("c"))
    {
        snackFee=8.90;
        System.out.println("your snack Fee is" +snackFee);
    }
    else
    {
        System.out.println("\nYou did not enter a correct option");
        System.out.println("Please try again using either A, B or C");
    }



    //calculate the total fee and total fee
    double result=movieFee+snackFee;  
    System.out.println("Final price is "+result);



}
//end main
Januka samaranyake
  • 2,385
  • 1
  • 28
  • 50
0
  1. Initialize variables or otherwise give them a value if no condition is met.

  2. Use equalsIgnoreCase() to compare strings.

  3. Display options and immediately make validations.

    import java.util.Scanner;
    public class MovieFee {
    public static void main(String[] args)
    {
    //scanner class
    Scanner input=new Scanner(System.in);
    
    //declare fee variables
    double movieFee = 0.0;
    double snackFee = 0.0;
    double couponFee = 0.0;
    
    do {
        //display the age options and prompt the user to enter one
        System.out.println("Child (<12)");
        System.out.println("Adult (12-65)");
        System.out.println("Senior (>65)");
        System.out.print("Choose your age category: ");
        String age=input.nextLine();
    
        //calculate the movie fee
        if(age.equalsIgnoreCase("child"))
        {
            movieFee=5.50;
        }
        else if(age.equalsIgnoreCase("adult"))
        {
            movieFee=8.50;
        }
        else if(age.equalsIgnoreCase("senior"))
        {
            movieFee=6.00;
        }
        else
        {
            System.out.println("\nYou did not enter a correct age group.");
            System.out.println("Please try again using one of: child, adult, senior\n");
        }
    } while(movieFee==0.0);
    
    do {    
        //display the snak bar options and prompt the user to enter one
        System.out.println("\nA - popcorn and pop");
        System.out.println("B - chocolate bar and pop");
        System.out.println("C - popcorn and candy and pop");
        System.out.print("What is your snack bar option(enter a letter): ");
        String snackOption=input.nextLine();
    
        //calculate the snack fee
        if(snackOption.equalsIgnoreCase("a"))
        {
            snackFee=7.90;
        }
        else if(snackOption.equalsIgnoreCase("b"))
        {
            snackFee=6.30;
        }
        else if(snackOption.equalsIgnoreCase("c"))
        {
            snackFee=8.90;
        }
        else
        {
            System.out.println("\nYou did not enter a correct option");
            System.out.println("Please try again using either A, B or C");
        }
    } while (snackFee==0.0);
    
    String coupon="";
    do {        
        //ask the user if he/she has a coupon    
        System.out.print("\nDo you have a coupon? Type yes or no: ");
        coupon=input.nextLine();
        if(coupon.equalsIgnoreCase("yes")) 
        {
            couponFee=2.0;
        }
        else if(coupon.equalsIgnoreCase("no"))
        {
            couponFee=0;
        }
    } while (!coupon.equalsIgnoreCase("yes") && !coupon.equalsIgnoreCase("no"));
    //calculate the total fee and total fee
    double result = movieFee + snackFee + couponFee; 
    System.out.println("Final price is "+result);
    }
    //end main    
    }
    //end class
    
SkyMaster
  • 1,323
  • 1
  • 8
  • 15