• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this gives me error cause the compiler is expecting a return out the curly brackets not inside
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • 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: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 4048
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!