• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

quick one: how can i put an if in a return

 
Ranch Hand
Posts: 607
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this gives me error cause the compiler is expecting a return out the curly brackets not inside
 
Marshal
Posts: 6215
502
IntelliJ IDE Python TypeScript Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to provide a return statement for all termination points out of the method. In your example you have not provided a return statement for when the if condition is false, i.e. OffOven is not less than 3.


As a side note: Java coding convention has method names and variable names as camelCase. So your syntax should be:
 
Giovanni Montano
Ranch Hand
Posts: 607
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:


HI Tim thanks this is clear but still cannot grasp this other aspect
let's imagine instead of a getter I want to evolve a setter in the program like that, into a routine that if the value is lower than 6 want the setter to go back to the Scanner function and ask again " what is the lenght of the pencil" take again the value and repeat the test, somebody suggested me that the goto in Java should be substitued by the while but in the case i need to use setter and getters to protect important functions, how I can simulate this goto?
 
Tim Cooke
Marshal
Posts: 6215
502
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a program where you want to do something "once or more" times then you'd use a do/while loop. So to ask the user for the pencil length repeatedly until length is greater than 6 you'd say:

Of course I am expecting an int to be entered and am making no attempt to handle any other type of input.
 
Giovanni Montano
Ranch Hand
Posts: 607
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:In a program where you want to do something "once or more" times then you'd use a do/while loop. So to ask the user for the pencil length repeatedly until length is greater than 6 you'd say:


Dear Tim, you are really kind BUT there is a big reason of disappointment from my side, I am hypothesizing that the function<6 is really serious so should be encapsulated, inside a private class in a way that nobody per mistake could change it. it is not this the function of getters and setters?
thank you again anyway
 
Marshal
Posts: 81618
593
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Sun coding conventions guide, which seems to have disappeared, frowned on returns in if statements. They suggestYou need to be careful with the ?: operator because it has low precedence and associates to the right, so it is slightly awkward to use. Obviously I don't know whether 99999 is the correct value.
 
Tim Cooke
Marshal
Posts: 6215
502
IntelliJ IDE Python TypeScript Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could encapsulate that rule out easily enough in a couple of ways. If you only need that rule inside this particular class then you could extract it as a private method like so:

Of if you're going to need that rule to be available to other classes then you can extract it into it's own class like so:


Getters and setters are mutation and access methods that simple get or set the value of a class member. Your method that decides whether some int is less that 6 is not a setter or a getter, it's just a function.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to put the validation inside the setter then the setter will have to throw an exception if an invalid value is passed ine.g.

You would then have to catch this exception in the loop where you call the setter.
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic