• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Need help - What is wrong in below code?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to print odd numbers between 2 numbers.
if left= 3, right = 9 then the program should print 3,5,7,9 and the program is working well.
but if left = 9 and right = 3, i.e left > right then the list is returning empty.
Can anyone of you please let me know what is the issue? Thank you.

 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Srividya Sri

check it
 
Srividya Sri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Swasthik and Sharma for providing the quick solution and the code is working just perfect. It would have been easy to understand if you could have explained me what was wrong in my code. I could print the list in line 35 (just before returning the list) but when I return the list it is returning empty. As mentioned by Swasthik, after adding return statement [return Between(left, right);] it is working fine.
 
Marshal
Posts: 76861
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I usually delete complete solutions; see what it says on the title page for this forum:-

We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

But I can see enough confusion that I think I shall let your suggestion stand for improvements.

What is wrong with iterating from 9 to 3 and getting an empty List? My first thought is, that would produce an empty List. What would you like to happen? What is to happen if you pass a negative number: what should the result be of passing −16 and −4, for example?
 
It runs on an internal combustion engine. This ad does not:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic