Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #18732
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | "bilsch" <king621@comcast.net> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: can't return value |
| Date | Thu, 13 Sep 2012 03:55:21 -0700 |
| Organization | A noiseless patient Spider |
| Lines | 62 |
| Message-ID | <k2se2q$fm9$1@dont-email.me> (permalink) |
| References | <k2pe2b$lp4$1@dont-email.me> <m3pq5r4e18.fsf@ipa.eternal-september.org> <k2rreo$hqj$1@dont-email.me> <k2s2vb$jr5$1@dont-email.me> |
| Injection-Date | Thu, 13 Sep 2012 10:55:22 +0000 (UTC) |
| Injection-Info | mx04.eternal-september.org; posting-host="5b8c2dd589928ab2c0d032da0393658a"; logging-data="16073"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX186SNR6rHHbE/9Co8yOcqJ0" |
| X-MimeOLE | Produced By Microsoft MimeOLE V6.00.2900.6157 |
| X-RFC2646 | Format=Flowed; Response |
| X-Newsreader | Microsoft Outlook Express 6.00.2900.5931 |
| Cancel-Lock | sha1:0OLLrP7zyfDVc3cX3/CvvM1v+iI= |
| X-Priority | 3 |
| X-MSMail-Priority | Normal |
| Xref | csiph.com comp.lang.java.programmer:18732 |
Show key headers only | View raw
"Stuart" <DerTopper@web.de> wrote in message
news:k2s2vb$jr5$1@dont-email.me...
> bilsch wrote in his first posting:
>> I want to get variable lastName for use in the main method (the whole
>> program isn't shown). I want to return it from inside the IF block within
>> the FOR loop.
>
> [snipped various replies]
>
> 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
>> don't know how to put it in.
>
> 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
> so:
>
>
> public static String lastName(String wholeName){
>
> for (int i = 0; i < wholeName.length(); i++){
> if (wholeName.charAt(i)== ' '){
> return wholeName.substring(i + 1);
> }
> }
> // If we reach this line, the parameter
> // wholeName cannot have contained a
> // blank, so we cannot extract the last
> // name.
> throw new exception ("cannot find lastname");
> }
>
> Alternatively, your method can simply return null:
>
> public static String lastName(String wholeName){
>
> for (int i = 0; i < wholeName.length(); i++){
> if (wholeName.charAt(i)== ' '){
> return wholeName.substring(i + 1);
> }
> }
> // If we reach this line, the parameter
> // wholeName cannot have contained a
> // blank, so we cannot extract the last
> // name.
> return null;
> }
>
>
> Which of these variants suits you better depends on how you want to use
> lastName. Both have their advantages and disadvantages.
>
> Regards,
> Stuart
Thanks for the info. I can't experiment with it right now. I'll post
later.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
can't return value "bilsch" <king621@comcast.net> - 2012-09-12 00:36 -0700
Re: can't return value Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-12 07:11 -0400
Re: can't return value bilsch <bilsch01@gmail.com> - 2012-09-12 22:15 -0700
Re: can't return value Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-13 08:53 -0400
Re: can't return value Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2012-09-12 15:32 +0300
Re: can't return value bilsch <bilsch01@gmail.com> - 2012-09-12 22:37 -0700
Re: can't return value Lew <lewbloch@gmail.com> - 2012-09-12 23:17 -0700
Re: can't return value markspace <-@.> - 2012-09-13 01:11 -0700
Re: can't return value Lew <lewbloch@gmail.com> - 2012-09-13 10:53 -0700
Re: can't return value Stuart <DerTopper@web.de> - 2012-09-13 09:45 +0200
Re: can't return value "bilsch" <king621@comcast.net> - 2012-09-13 03:55 -0700
Re: can't return value Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2012-09-13 15:06 +0300
Re: can't return value Stuart <DerTopper@web.de> - 2012-09-13 20:53 +0200
Re: can't return value Roedy Green <see_website@mindprod.com.invalid> - 2012-09-12 20:54 -0700
csiph-web