Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Jukka Lahtinen Newsgroups: comp.lang.java.programmer Subject: Re: can't return value Date: Wed, 12 Sep 2012 15:32:51 +0300 Organization: A noiseless patient Spider Lines: 34 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit Injection-Info: ipa.eternal-september.org; posting-host="a5a3e65df26a45e8ef56e0325d9681f2"; logging-data="21497"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//TcXIU3fKM/k21xckLaZO" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.1 (gnu/linux) Cancel-Lock: sha1:6lld2A4CkSBFvCoSStgQAuG7HPQ= sha1:jLMSJCTGqOCsJ8oQYSoXXqeiwaY= X-no-archive: yes Xref: csiph.com comp.lang.java.programmer:18668 "bilsch" 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. -- Jukka Lahtinen