• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializing an int variable before if statement  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When compiling i get an Error stating that my variable has not been initialized.


Why is number not initialized?

I put int number = 0;
But then when i print, no matter the value of input it stays at 0?


 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael rust wrote:
I put int number = 0;
But then when i print, no matter the value of input it stays at 0?

Right now you don't have that. You have:

That doesn't give it a default. And since the if chains don't have else statements, Java doesn't know if you've covered all the paths.
 
Michael rust
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
Michael rust wrote:
I put int number = 0;
But then when i print, no matter the value of input it stays at 0?

Right now you don't have that. You have:

That doesn't give it a default. And since the if chains don't have else statements, Java doesn't know if you've covered all the paths.


I tried with int number = 0;
Nothing changes
 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the value of isValid, and what is the value of "input" ?
Also, you have a lot of statement like:
and they are probably all incorrect.
The second part is meaningless, since if input is 1 then it is surely less than or equal to 21, so what's the point?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, never write == true or == false which are both poor style and error‑prone. It is only a matter of time before you write = by mistake and you have now got two logic errors for the price of one.
Not
if (b == true) ...
but
if (b) ...
Not
if (b == false) ...
but
if (!b) ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!