This week's book giveaway is in the XML and Related Technologies forum.
We're giving away four copies of Java XML & JSON and have Jeff Friesen on-line!
See this thread for details.
Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Is it possible to use the "return" keyword in middle of if statement or a loop?  RSS feed

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, i'm sometimes asked of methods that require a boolean value as an answer. and when i am writing the method, when i try to return true\false in the end of the loop/conditional statement, it says " missing return statement"
so in that case i am using a String variable and store there a value that i get from the loop\condition statement . and after the loop/condition statement  ends  i'm using a condition statement as a return value.
i want to save those code lines and not use a String value to store the boolean value. is it possible?
i will give you an example:

is there another option so  solve it without using the variable?
Thank you guys !
 
Saloon Keeper
Posts: 2321
293
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rian bron wrote:i am using a String variable and store there a value that i get from the loop\condition statement . and after the loop/condition statement  ends  i'm using a condition statement as a return value.
i want to save those code lines and not use a String value to store the boolean value. is it possible?


Why not use a boolean to hold your answer, and avoid the String altogether?
BTW: All those Date methods are deprecated.
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

rian bron wrote:i am using a String variable and store there a value that i get from the loop\condition statement . and after the loop/condition statement  ends  i'm using a condition statement as a return value.
i want to save those code lines and not use a String value to store the boolean value. is it possible?


Why not use a boolean to hold your answer, and avoid the String altogether?
BTW: All those Date methods are deprecated.



yeah it is also an option.
but my question is if there is a way to avoid using these stored variables inside a loop/condition, and use the  "return " keyword  instead, or is it not possible?
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and why these date methods are deprecated?
 
Ron McLeod
Saloon Keeper
Posts: 2321
293
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rian bron wrote:and why these date methods are deprecated?



https://docs.oracle.com/javase/8/docs/api/java/util/Date.html wrote:Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of dates as year, month, day, hour, minute, and second values. It also allowed the formatting and parsing of date strings. Unfortunately, the API for these functions was not amenable to internationalization. As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Date are deprecated.

 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

rian bron wrote:and why these date methods are deprecated?



https://docs.oracle.com/javase/8/docs/api/java/util/Date.html wrote:Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of dates as year, month, day, hour, minute, and second values. It also allowed the formatting and parsing of date strings. Unfortunately, the API for these functions was not amenable to internationalization. As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Date are deprecated.




oh i see, but anyways the Dates methods i wrote was  from a question from a test, i didn't mean to referred it to an existing JDK file. lol thank you for the remark anyways:)
 
Ron McLeod
Saloon Keeper
Posts: 2321
293
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rian bron wrote:my question is if there is a way to avoid using these stored variables inside a loop/condition, and use the  "return " keyword  instead, or is it not possible?


Sure, you could return after each test, but some consider it a bad practice to have methods with multiple returns.
 
Saloon Keeper
Posts: 5456
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rian bron wrote:and why these date methods are deprecated?

Because the Date class was a triumph of bad design, but it was superseded by Calendar which was hardly any better.
By the way, please explain the logic of your later() method, which I think is incorrectly implemented.
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

rian bron wrote:and why these date methods are deprecated?

Because the Date class was a triumph of bad design, but it was superseded by Calendar which was hardly any better.
By the way, please explain the logic of your later() method, which I think is incorrectly implemented.



Where did you see i wrote a later() method?
and thank you for the previous methods you gave me
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry; I was mistaken: after() method
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!