• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Beginning "While" Program Help

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you just want to loop until correct is false:

while (correct) {
. . .
}
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variable input is not initialized and your main method is using variable scan
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it! Thank you all for your help with my n00b questions.
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic