• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Beginning "While" Program Help

 
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?


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • 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.



 
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variable input is not initialized and your main method is using variable scan
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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.
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic