• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to structure this function?  RSS feed

 
Varuna Seneviratna
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lot is a class.
lots is a ArrayList
The Lot class has a int number instance variable which is returned by the
getName() function.

I want to return a Lot object which has the number value received by the function through the number parameter.The problem is how to place the return statement.When the class is compiled a syntax error is displayed as "missing return statement"

 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, why the compiler is giving the missing return statement error is that all the cases for the return is not considered. You have taken care of returning the values if the for loop is executed, but you missed considering the case when the for loop is not executed, in which case it is not returning anything. right?
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EDIT: lol, was beaten to it... will leave this reply anyway.

I suspect it is because you are returning within your loop. Therefore it is possible that there is nothing in the "lots" list, so it might never get into the while loop.

Another problem you will have is that if your first if statement is false then you will go to the else which will return null, therefore never ending your loop.

You could do something like:



I haven't compiled it, but should be fine

Some people wouldn't use the "break" call, personally I don't see an issue with it, but up to you really.

If you can though I would suggest using JDK 1.5+ and using generics to make things a bit nicer.

Also I would suggest not putting your system out in this function.

HTH
Ror

[ November 03, 2008: Message edited by: Rory Marquis ]
[ November 03, 2008: Message edited by: Rory Marquis ]
 
Varuna Seneviratna
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rory Marquis:
[qb]EDIT: lol, was beaten to it... will leave this reply anyway.


What do you mean by the above statement?



Another problem you will have is that if your first if statement is false then you will go to the else which will return null, therefore never ending your loop.


doesn't the return statment exits the function?

You could do something like:



I haven't compiled it, but should be fine

I also came to the conclusion you have suggested But I wonder whether there is a better way.Anyway Thanks









Also I would suggest not putting your system out in this function.

What do you intend by this above sentence also.






Regards Varuna
[ November 03, 2008: Message edited by: Varuna Seneviratna ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option would be

 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja is also correct, that would be fine too. I only did it the way I did because I am beaten with the coding standards stick which forces me to only return at the end of a method to make it more readable ^_^


Originally posted by Rory Marquis:
EDIT: lol, was beaten to it... will leave this reply anyway.


What do you mean by the above statement?



That someone else got in a reply to your question before me.






doesn't the return statment exits the function?



yes it does, however, your original had an if else, and you said if it isn't the lot you are looking for then return null. Chances are good that it isn't the first one in the list every time, otherwise you wouldn't be looking for it


I also came to the conclusion you have suggested But I wonder whether there is a better way.Anyway Thanks


There is only a limited number of ways to iterate though a collection and get the result.

The better way would be to use generics, then you could use a For Each loop... but you would still have to return outside the loop if you lot was not found.

quote:
Also I would suggest not putting your system out in this function.


What do you intend by this above sentence also.


This function is a doing function it is perfectly fine to put logging into it, but I would consider it far better design not to put outputs into a function like this. Rather the function that calls it, the controller, of your application should be responsible for identifying the null return and deciding whether it should tell the user, or log the result or both.

HTH
Ror
[ November 04, 2008: Message edited by: Rory Marquis ]
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rory Marquis:
EDIT: lol, was beaten to it... will leave this reply anyway.

Some people wouldn't use the "break" call, personally I don't see an issue with it, but up to you really.


Quite right to leave the reply; it adds to the value of the discussion.

You can avoid the "break" like this, which is mostly copied from your post:These methods will of course only find the first of two identical Lots in the same List.
[edit]Delete "not"[/edit]
[ November 04, 2008: Message edited by: Campbell Ritchie ]
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell you make a good point.

If there are supposed to be duplicates, then you should think about returning a sub set of the lots list.

However, if there are not supposed to be duplicates, I would consider to replace your List with a Set.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!