Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: "bilsch" Newsgroups: comp.lang.java.programmer Subject: can't return value Date: Wed, 12 Sep 2012 00:36:41 -0700 Organization: A noiseless patient Spider Lines: 33 Message-ID: Injection-Date: Wed, 12 Sep 2012 07:36:43 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="5b8c2dd589928ab2c0d032da0393658a"; logging-data="22308"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++gGqqxgK08xhPO94LqG7b" X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 X-RFC2646: Format=Flowed; Original X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 Cancel-Lock: sha1:IkW6EzGTZl4laAZ2E8E6kjsC3WA= X-Priority: 3 X-MSMail-Priority: Normal Xref: csiph.com comp.lang.java.programmer:18666 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. 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); } }