• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initialization and "While"  RSS feed

 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to initialize variable commissionPay, but there's something wrong. Why does this not work and what can I do to solve it?

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:I'm trying to initialize variable commissionPay, but there's something wrong. Why does this not work and what can I do to solve it?

Can you tell us what 'something' is wrong and what it means to not work? TellTheDetails (<- link) and you will get better help.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eclipse is telling me that commissionPay is a duplicate local variable. The only other place I use this is still initialize it.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yeah. You're already declaring it above -- why are trying to declare a new variable of the same name (and then discarding it)?

If you simply want to set the value of the variable, you do not declare it again. Simply assign it a value.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried removing the first commissionPay as well. In that case I receive a "commissionPay cannot be resolved to a variable" error

 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're declaring your comissionPay variable on line 31, but you're declaring it again on lines 43, 46 and 49. You're not making the same mistake with your comissionRate variable though.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Facepalm] Geez, how did I miss that? Thanks.

I still seem to stuck in an infinite loop. When I enter -999, I am asked what my sales are. How can break out of it?
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the variables you're using. The loop will go on as long as your end variable is not -999, but what you're reading from your Scanner is stored in a variable named amount. Your end variable never changes, so the loop will go on forever.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it! Thank you all for your help with my n00b questions.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!