• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Boolean method not executed

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me why the boolean method that's supposed to check the input is not "Bye" isn't executing?




thanks
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What isn't working, do you mean you're in an invinite loop? Cause that would make sence.
Furthermore don't declare your string variablename in all caps, all caps are for final variables
 
Kevin O'Sullivan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:What isn't working, do you mean you're in an invinite loop? Cause that would make sence.
Furthermore don't declare your string variablename in all caps, all caps are for final variables



Well If you type bye its suppose to break out of the loop and exit the program, I'm trying to get use to putting everything into its own method but maybe you don't have to do that .
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The prob is your while loop, you return true or false based on the user's input, but nowhere you use that value
eather change it to:

or assign the return value to a variable and use that in your while loop.
 
Marshal
Posts: 74037
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, why is that String variable declared as public and static? I am sure both of those are mistakes.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Also, why is that String variable declared as public and static? I am sure both of those are mistakes.


it even could be a local variable, but the comment about being static could be maded for all methods too
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how I would have written it:

MainClass:

MethodClass:
 
Kevin O'Sullivan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Also, why is that String variable declared as public and static? I am sure both of those are mistakes.



Why do you think it's a mistake? I wanted it to be a global variable, so I could reuse it.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's considered bad practise.In this case there is no need to reuse it, we don't see the rest of your code of course, but if you check my code, you'll see I got rid of it all together.
If you really need to reuse it, make an instance variable of it and provide a getter method
 
Kevin O'Sullivan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:It's considered bad practise.In this case there is no need to reuse it, we don't see the rest of your code of course, but if you check my code, you'll see I got rid of it all together.
If you really need to reuse it, make an instance variable of it and provide a getter method



Cheers Daniel.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keep in mind that when you use a getter method to get the instance variable and change it's vallue, the instance variable will not change
so:
 
Campbell Ritchie
Marshal
Posts: 74037
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:. . . Why do you think it's a mistake?

  • 1: Making it static means there is only one instance, so you can only have one question.
  • 2: Making it public allows any code to reassign it, so the question can change while you are not watching.
  • I wanted it to be a global variable, so I could reuse it.

  • 3: Global variables are notoriously error‑prone, because no code has any control over whether they are changed.
  •  
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic