Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > can't return value

Reply
Thread Tools

can't return value

 
 
bilsch
Guest
Posts: n/a
 
      09-12-2012
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);
}
}


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      09-12-2012
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);
> }
> }
>
>



--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
"The speed at which the system fails is usually not important."
 
Reply With Quote
 
 
 
 
Jukka Lahtinen
Guest
Posts: n/a
 
      09-12-2012
"bilsch" <(E-Mail Removed)> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-13-2012
On Wed, 12 Sep 2012 00:36:41 -0700, "bilsch" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

> there is no RETURN statemen


you have to make sure there is a some sort of return on every possible
way to exit. Look at both branches of your IF. Whether you leave
early or fall out the bottom of the loop. Make sure there is no way
you can even theoretically exit without a value.
--
Roedy Green Canadian Mind Products http://mindprod.com
A new scientific truth does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04


 
Reply With Quote
 
bilsch
Guest
Posts: n/a
 
      09-13-2012
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.
 
Reply With Quote
 
bilsch
Guest
Posts: n/a
 
      09-13-2012
On 9/12/2012 5:32 AM, Jukka Lahtinen wrote:
> "bilsch"<(E-Mail Removed)> 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.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-13-2012
bilsch wrote:
> 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.


That's a meaningless request. You can't throw an exception if
the program cannot run, or even compile.

Once you fix the compilation error, you won't be missing a
'return'.

And there are no 'RETURN' statements in Java.

If you read the Java tutorials on the Oracle site, they
explain the basics of Java syntax and what constitutes
a legal Java program.

--
Lew
 
Reply With Quote
 
Stuart
Guest
Posts: n/a
 
      09-13-2012
bilsch wrote in his first posting:
> 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.


[snipped various replies]

On 9/13/12 bilsch wrote:
> 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.


What Jukka probably meant to say is that you could throw an exception in
case your method could not extract a surname from the passed string.
Like so:


public static String lastName(String wholeName){

for (int i = 0; i < wholeName.length(); i++){
if (wholeName.charAt(i)== ' '){
return wholeName.substring(i + 1);
}
}
// If we reach this line, the parameter
// wholeName cannot have contained a
// blank, so we cannot extract the last
// name.
throw new exception ("cannot find lastname");
}

Alternatively, your method can simply return null:

public static String lastName(String wholeName){

for (int i = 0; i < wholeName.length(); i++){
if (wholeName.charAt(i)== ' '){
return wholeName.substring(i + 1);
}
}
// If we reach this line, the parameter
// wholeName cannot have contained a
// blank, so we cannot extract the last
// name.
return null;
}


Which of these variants suits you better depends on how you want to use
lastName. Both have their advantages and disadvantages.

Regards,
Stuart
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      09-13-2012
On 9/12/2012 11:17 PM, Lew wrote:
> bilsch wrote:
>> 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.


>
> And there are no 'RETURN' statements in Java.



Not sure where this came from, I didn't see anyone upthread quote return
as 'RETURN'. However, the null literal in Java is properly spelled
'null', not NULL. Perhaps that's what you meant to comment on.


 
Reply With Quote
 
bilsch
Guest
Posts: n/a
 
      09-13-2012

"Stuart" <(E-Mail Removed)> wrote in message
news:k2s2vb$jr5$(E-Mail Removed)...
> bilsch wrote in his first posting:
>> 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.

>
> [snipped various replies]
>
> On 9/13/12 bilsch wrote:
>> 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.

>
> What Jukka probably meant to say is that you could throw an exception in
> case your method could not extract a surname from the passed string. Like
> so:
>
>
> public static String lastName(String wholeName){
>
> for (int i = 0; i < wholeName.length(); i++){
> if (wholeName.charAt(i)== ' '){
> return wholeName.substring(i + 1);
> }
> }
> // If we reach this line, the parameter
> // wholeName cannot have contained a
> // blank, so we cannot extract the last
> // name.
> throw new exception ("cannot find lastname");
> }
>
> Alternatively, your method can simply return null:
>
> public static String lastName(String wholeName){
>
> for (int i = 0; i < wholeName.length(); i++){
> if (wholeName.charAt(i)== ' '){
> return wholeName.substring(i + 1);
> }
> }
> // If we reach this line, the parameter
> // wholeName cannot have contained a
> // blank, so we cannot extract the last
> // name.
> return null;
> }
>
>
> Which of these variants suits you better depends on how you want to use
> lastName. Both have their advantages and disadvantages.
>
> Regards,
> Stuart


Thanks for the info. I can't experiment with it right now. I'll post
later.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function take in value and return value CSINVA ASP .Net 6 11-07-2007 12:49 PM
Getting ID, calling url, search for value, return value Tim Fröglich ASP .Net Web Services 1 01-10-2006 09:18 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM



Advertisments