The compiler just wants to make sure that your variables have been assigned a value before you use them in an expression. If you assign them a default value when you declare them, the compiler will be happy
double fees = 0;
as you noted.
Ezzaral
null
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28
You are supplying the total to the displayTotal(double total) method, so why are you making a separate calculation (with empty variables) for the total that you are printing out
System.out.println("The total is: "+ tuition +".");
Ezzaral
null
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28
Well, look at tuition. Apparently it's 0.0 when you print it. What happens to it before you print it? How does it get that value? And what happens before that? How does it get the preceding value? Work back through the code this way and you should be able to spot it.
In this case, you should be able to spot it in about one hop backwards.
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
Skill Endorsements: 4
I think you may be confused about variable scope. The "fees" you declared in main() is not the same variable that you are working with here
public static void displayTotal(double total)
{
DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
[B]double fees;[/B]
double tuition;
fees += tuition;
System.out.println("The total is: "+ tuition +".");
}
In displayTotal, you have delared a new and completely separate varialbe "fees" that is only visible within that method declaration.
Edit: Glad you got it figured.
Ezzaral
null
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28
Question Answered as of 2 Years Ago by
Ezzaral
and
jon.kiparsky my professor does not allow us to use a textbook or anything like that
I hope he's got some redeeming qualities. He sounds like an idiot from over here.
You can learn a lot from reading through the language spec. It's not an easy read, and you'll probably puzzle over a lot of it, but you can get it free on line.
For example, this section tells you what you need to know about scope - but it's going to take a bit of work to understand it, if you're new to the language.
It's work worth doing, but it's still work.
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
Skill Endorsements: 4