Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > can't return value

Reply
Thread Tools

can't return value

 
 
Jukka Lahtinen
Guest
Posts: n/a
 
      09-13-2012
Stuart <(E-Mail Removed)> writes:
> On 9/13/12 bilsch wrote:
>> Based on what Jukka said I got it to work without using the for loop.
>> OK. Now I see what Jukka meant by initializing to NULL. But I don't
>> know what exception to throw for 'misssing return statement'. Also I


> What Jukka probably meant to say is that you could throw an exception in
> case your method could not extract a surname from the passed string. Like


No, I just meant that the original code WILL throw an
ArrayIndexOutOfBoundsException if it doesn't find a space.
Right here in the String#charAt method, when i reaches wholeName.length():

for (int i = 0; i <= wholeName.length(); i++){
if (wholeName.charAt(i)== ' '){

and bilsh probably wants to avoid it.

--
Jukka Lahtinen
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      09-13-2012
On 9/13/2012 1:15 AM, bilsch wrote:
> On 9/12/2012 4:11 AM, Eric Sosman wrote:
>> On 9/12/2012 3:36 AM, bilsch wrote:
>>>[...]

>> You need to decide what to do if there is no space in
>> `wholeName': throw an exception, return `null', whatever you
>> like -- but the compiler will not allow you to just ignore
>> the possibility.
>> [...]

> thanks for your reply. I got it to work two different ways. Could you
> explain returning NULL? Also, I don't know what exception to throw or
> how to do it.


Any variable that refers to an object -- a String, a List,
or whatever -- can have the special value `null' (not `NULL')
to indicate that it "refers to nothing" at the moment. Methods
that return object references can return `null' to indicate
"I've got nothing to give you." If your method returns, it
must return some value; I'm suggesting that if your method
could not do its job, `null' is a value you might consider
returning. It's just a special value you might decide should
mean "I couldn't find a last name in `wholeName'."

Another possibility is to throw an exception: The method
tries to find a last name, discovers that `wholeName' doesn't
contain one, and says "Hey, stupid caller: You fed me garbage!"
In the case at hand, IllegalArgumentException seems a likely
candidate, so the method could announce its displeasure with

throw new IllegalArgumentException(
"no last name in " + wholeName);

When a method terminates by throwing an exception it does not
need to return a value, because in truth it doesn't "return"
at all: It abruptly stops what it was doing, and what all its
callers were doing, up to the point where some caller has a
`try {...} catch' for the type of exception thrown.

Bilsch, this is very elementary stuff, the sort of thing
you will find in any introductory textbook or tutorial on Java.
I suggest you consult one; trying to learn the language one
corrected blunder at a time is not very efficient. You might
also think about using comp.lang.java.help for elementary
questions; comp.lang.java.programmer is (in theory) a forum
for people who already know the basics and are tackling more
advanced issues. (Note the "in theory.")

CC'ed, and follow-ups set.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
"The speed at which the system fails is usually not important."
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      09-13-2012
markspace wrote:
> Lew wrote:
>> bilsch wrote:
>>> OK. Now I see what Jukka meant by initializing to NULL. But I don't
>>> know what exception to throw for 'misssing return statement'. Also I
>>> don't know how to put it in.

>
>> And there are no 'RETURN' statements in Java.

>
> Not sure where this came from, I didn't see anyone upthread quote return
> as 'RETURN'. However, the null literal in Java is properly spelled
> 'null', not NULL. Perhaps that's what you meant to comment on.


No, I was commenting on the OP's very first post that started this thread:
"The variable lastName isn't visible to the RETURN statement
that's located two brackets lower."

--
Lew
 
Reply With Quote
 
Stuart
Guest
Posts: n/a
 
      09-13-2012
On 9/13/12 bilsch wrote:
>>> Based on what Jukka said I got it to work without using the for loop.
>>> OK. Now I see what Jukka meant by initializing to NULL. But I don't
>>> know what exception to throw for 'misssing return statement'. Also I


Stuart writes:
>> What Jukka probably meant to say is that you could throw an exception in
>> case your method could not extract a surname from the passed string. Like


On 9/13/12 Jukka Lahtinen wrote:
> No, I just meant that the original code WILL throw an
> ArrayIndexOutOfBoundsException if it doesn't find a space.
> Right here in the String#charAt method, when i reaches wholeName.length():
>
> for (int i = 0; i <= wholeName.length(); i++){
> if (wholeName.charAt(i)== ' '){
>
> and bilsh probably wants to avoid it.


Now I'm glad that I -- as an afterthought -- slipped the word "probably"
into my sentence. Well, that's what one gets if one doesn't bother to
check out the context (which was unfortunately missing).

Regards,
Stuart


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function take in value and return value CSINVA ASP .Net 6 11-07-2007 12:49 PM
Getting ID, calling url, search for value, return value Tim Fröglich ASP .Net Web Services 1 01-10-2006 09:18 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM



Advertisments