Path: csiph.com!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: bilsch Newsgroups: comp.lang.java.programmer Subject: Re: can't return value Date: Wed, 12 Sep 2012 22:15:00 -0700 Organization: A noiseless patient Spider Lines: 72 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 13 Sep 2012 05:15:01 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="5b8c2dd589928ab2c0d032da0393658a"; logging-data="12368"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jxafApg5npj+XTlRUPoLP" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 In-Reply-To: Cancel-Lock: sha1:0eyy/ehEjddh9FLVEq5P82QRMgw= Xref: csiph.com comp.lang.java.programmer:18722 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.