Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #18722
| From | bilsch <bilsch01@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: can't return value |
| Date | 2012-09-12 22:15 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <k2rq4k$c2g$1@dont-email.me> (permalink) |
| References | <k2pe2b$lp4$1@dont-email.me> <k2pql5$o74$1@dont-email.me> |
On 9/12/2012 4:11 AM, Eric Sosman wrote:
> On 9/12/2012 3:36 AM, bilsch wrote:
>> 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. The compiler complains there is no RETURN statement when I
>> have it there. It stops complaining if I put RETURN two brackets lower
>> (notice it is commented out there). The variable lastName isn't
>> visible to
>> the RETURN statement that's located two brackets lower. The variable
>> lastName is only visible inside the FOR / IF block.
>>
>> 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.
>
> Although your method contains a `return' statement, the
> compiler has noticed that the `return' might not always be
> executed. Since you only execute the `return' after detecting
> a space character in `wholeName', what happens if `wholeName'
> contains no spaces? Answer: The `for' loop completes[*]
> without ever reaching the `return', and then where are you?
>
> 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.
>
> [*] Actually, the loop will not complete. If there is
> no space in `wholeName', you will eventually get an exception
> when you call charAt() with an argument that's beyond the
> end of the string. In fact, then, the analysis I've given
> above is not quite correct: execution will never get to where
> the commented-out `return' is. But (1) the compiler cannot
> "see" the fact that charAt() will throw, and (2) if you fix
> the error in the `for' loop the analysis becomes correct again.
>
> Aside: There are at least two easier ways to find the
> pieces of `wholeName'. The String class has indexOf() and
> lastIndexOf() methods that can find the space character for
> you, and also has a split() method that can both find the
> spaces and chop the string into space-separated pieces.
>
> Aside II: Re-read the documentation of the substring()
> method; there's a surprise awaiting you.
>
>> 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;
>> }
>>
>>
>> public static void main(String[] args){
>> String wholeName = "Bill Jones", last;
>> last = lastName(wholeName);
>> System.out.println("lastname is: " + last);
>> }
>> }
>>
>>
>
>
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.
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