• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginning "While" Program Help  RSS feed

 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to figure out what's wrong with the code below. Could someone give me a nudge in the right direction please?


 
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 figure out what's wrong with the code below. Could someone give me a nudge in the right direction please?

Why do you think there is something wrong with the code? Do you get an error? Does it misbehave when you run it? TellTheDetails please.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For "correct != 999, I'm told that "The operator != is undefined for the argument type(s) boolean, int". For guess=999, I'm told "Type mismatch: cannot convert from int to boolean". In the directions in the code, I'm supposed to assign the bollean to false, so I'm not sure how to proceed.



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable correct is declared as a boolean. So it can only attain the values true and false. 999 is not a boolean.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just want to loop until correct is false:

while (correct) {
. . .
}
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also please take better care with your indentation and matching up braces. That's an important tool for inspecting the structure of the program.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've made a lot of progress. I am able to reach the point where I am asked if I want to play again and I get this stack trace.

Exception in thread "main" java.lang.NullPointerException
at itp120mod3abstart.GuessingGame.main(GuessingGame.java:42)

How I use input.nextLine() correctly here? I need to be able to say "yes" in order to restart, else the program stops.

 
Saket Barve
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variable input is not initialized and your main method is using variable scan
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:I'm trying to figure out what's wrong with the code below.

Sam,

Please DontWriteLongLines.
It makes your thread very hard to read, and it's actually bad coding practice.

I'd break yours up myself, but you have tons of them (and in more than one post), so perhaps you could do it?
Just use the 'Edit' button.

And for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments AND long method calls.

Thanks.

Winston
 
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!