0

I am a student, just learning about Class and Methods. I am receiving an error (line 30 - savings = pr * discount / 100) "Cannot find symbol". I understand that my variable discount, is out of scope, but I cannot figure out how to correct this. I have followed the instructions provided to me, but it still is not working. I have already caught a few typos in the textbook, so is there something missing? Or is it my positioning of curly brackets?

import java.util.Scanner; // Allows for user input

public class ParadiseInfo2
    {
    public static void main(String[] args)
    {
        double price; // Variable for minimum price for discount
        double discount; // Variable for discount rate
        double savings; // Scanner object to use for keyboard input
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter cutoff price for discount >> "); 
        price = keyboard.nextDouble();
        System.out.print("Enter discount rate as a whole number >> ");
        discount = keyboard.nextDouble();

        displayInfo();

        savings = computeDiscountInfo(price, discount); 


    System.out.println("Special this week on any service over " + price);
    System.out.println("Discount of " + discount + " percent");
    System.out.println("That's a savings of at least $" + savings);

    }

    public static double computeDiscountInfo(double pr, double dscnt)
    {
    double savings;
    savings = pr * discount / 100;
    return savings;
    }

    public static void displayInfo()
    {   
    System.out.println("Paradise Day Spa wants to pamper you.");
    System.out.println("We will make you look good.");  
    }   
}   
5
  • It's a simple problem to fix. I added an answer below. Commented Sep 16, 2017 at 16:32
  • By the way, if my answer solved the problem - may I ask you kindly accept the answer by clicking on the grey check mark next to it, making it green? Commented Sep 16, 2017 at 16:35
  • 2
    Let this be a lesson on why removing vowels from variable names is a terrible idea. Commented Sep 16, 2017 at 16:45
  • Voting to close on the grounds that this is a typo and thus of little value to future readers. Commented Sep 16, 2017 at 16:45
  • The shortened variable names were exactly how the textbook explained. I thought it was a bit odd, but thought there would be an explanation in the textbook later. Commented Sep 17, 2017 at 14:32

2 Answers 2

1

Your code is correct - you just called the out of scope variable discount when you needed the in scope variable dscnt. Try this:

public static double computeDiscountInfo(double pr, double dscnt) {
    double savings;
    savings = pr * dscnt / 100;
    return savings;
}
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is caused by, as you mentioned in your question, the variable discount being out of scope. Let's look at why.

In your original code, the method computeDiscountInfo(double pr, double dscnt) is passed to parameters: a double labeled pr, and another double labeled dscnt. your method will only have knowledge of these two parameters, and not of anything going on outside of it. (There are some exceptions to this, such as 'static' variables, or variables passed from a parent. However, these are most likely beyond the scope of your learning at the moment. I'm sure you wil cover them in school soon.)

Since the variable discount is declared in the main() method, there is no way for your computeDiscountInfo(double pr, double dscnt) method to know of it's existence. When you cal on this method, you can pass it the discount variable as a parameter to be used (as you do in your code with savings = computeDiscountInfo(price, discount);) The method will then apply the value of discount to it's own local variable dscnt that you defined in the method's declaration. This is the variable that the method will know and use.

Now, lets look back at your method:

public static double computeDiscountInfo(double pr, double dscnt)
    {
    double savings;
    savings = pr * discount / 100;
    return savings;
    }

In this method you refer to the variable as discount, not by the local name dscnt as declared in the method's parameters. The method has no understanding of what discount means. It could be passed any double in this place. By changing the word discount to dscnt inside your method, the method will be able to understand what you are reffering to and use the value properly.

public static double computeDiscountInfo(double pr, double dscnt)
    {
    double savings;
    savings = pr * dscnt/ 100;
    return savings;
    }

I hope that this makes sense to you, please let me know if it does not. The concepts of local variables and variable scope is key parts of the foundation of Object-Oriented Programming.

2 Comments

Thank you so much for the wonderful explanation! Yes, it makes sense. I appreciate your help!
Glad I was able to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.