Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18724

Re: can't return value

From bilsch <bilsch01@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: can't return value
Date 2012-09-12 22:37 -0700
Organization A noiseless patient Spider
Message-ID <k2rreo$hqj$1@dont-email.me> (permalink)
References <k2pe2b$lp4$1@dont-email.me> <m3pq5r4e18.fsf@ipa.eternal-september.org>

Show all headers | View raw


On 9/12/2012 5:32 AM, Jukka Lahtinen wrote:
> "bilsch"<king621@comcast.net>  writes:
>
>> I thought passing variables was a way to get around the visibility problem -
>> but I'm stuck anyway.  Does anyone have a suggestion?  TIA  Bill S.
>>
>> public class Name01{
>>
>>      public static String lastName(String wholeName){
>>       for (int i = 0; i<= wholeName.length(); i++){
>>           if (wholeName.charAt(i)== ' '){
>>              String lastName = wholeName.substring(i,wholeName.length()-1);
>>              //System.out.println(lastName);
>>              return lastName;
>>           }
>>       }
>>       //return lastName;
>>      }
>
> What Eric wrote in his followup, and you could also define lastName
> before the for loop and initialize it to null.
>
>     public static String lastName(String wholeName) {
>       String lastName = null;
>       for (int i = 0; i<  wholeName.length(); i++) {
> ...
>
> Also notice that you probably should change the<= operator to<  in the
> for loop condition to avoid StringIndexOutOfBoundsException if there's
> no space in the parameter String.
> And like Eric wrote, there are handier ways to split a String, but
> that's not where your current problem is.


Thanks for your reply.
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.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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