• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

missing return statement error message when compiling class

 
Ranch Hand
Posts: 40
MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whilst attempting an exercise in the SCJP study guide - (Exercise 5-3, pages 352 - 353), I get the following error message on compilation:

Propagate.java:15: missing return statement
}
^
1 error

Below is the relevant part of the class that I've written.



Clearly reverseStr IS being returned - immediately after the for loop - so whats going on??
[ March 08, 2007: Message edited by: Stuart Lord ]
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, the problem is with the location of the return statement. Since it is within the if() loop, there is a possibility of the routine running without returning anything, and this is forbidden.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would make the following changes:

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With other words: what should the method return if in.length() <= 0?
 
Stuart Lord
Ranch Hand
Posts: 40
MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks for this Pete - I tried your solution and got rid of the error message. Clearly the problem lay with the scope of reverseStr - i.e.it had to be accessible outside of the if() construct.

To answer Ilja's question - the object of the exercise is to write a class that demonstrates exception handling and so if no argument was entered the length of in would be 0. This throws the ArrayIndexOutOfBoundsException exception, and so the catch should be trapping that first.

I've amended the class so as to display the value of reverseStr outside of the try/catch/finally block, but am getting the following compilation error:

Propagate.java:34: reverse(java.lang.String) in Propagate cannot be applied to (
)
System.out.println("The new string is " + reverse());
^
1 error

I'll include the entire class below:

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, I'd change this:

to this:


Next, you have to either change your reverse method to throw ArrayIndexOutOfBoundsException or change your catch statement to catch all exceptions.

Finally, you can't call reverse() without a parameter as you do here:


Consider using a string variable.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you have to either change your reverse method to throw ArrayIndexOutOfBoundsException or change your catch statement to catch all exceptions.


Why throwing ArrayIndexOutOfBoundsException ? This is a RuntimeException. I don't think you should deal with it.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stuart Lord:
Many thanks for this Pete - I tried your solution and got rid of the error message. Clearly the problem lay with the scope of reverseStr - i.e.it had to be accessible outside of the if() construct.

To answer Ilja's question - the object of the exercise is to write a class that demonstrates exception handling and so if no argument was entered the length of in would be 0. This throws the ArrayIndexOutOfBoundsException exception, and so the catch should be trapping that first.


Exactly where is that exception being thrown?
 
Stuart Lord
Ranch Hand
Posts: 40
MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a few points to clear up here:

First: You can see whats in reverseStr by adding the line

immediately after the if() construct in the reverse method and yes, the end condition should be

Secondly: To simplify matters I changed the catch statement to read and deleted the and then ran the code with no parameter - i.e. java Propagate the catch trapped the exception with the report:

The string's empty!!
0
java.lang.ArrayIndexOutOfBoundsException: 0
at Propagate.main(Propagate.java:22)
End of main method..


So whats being trapped is the ArrayIndexOutOfBoundsException and the catch should be changed to trap this first. The exception is being thrown in the method and is 'caught' by the catch but as a 'general' exception.
 
Stuart Lord
Ranch Hand
Posts: 40
MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks to all who have contributed with their help and suggestions - just 2 final things - the value of reverseStr is displayed follows:


(as per Pete's suggestion)

and secondly regarding Satou's remark - in this class this exception is trapped in a catch statement - but not thrown. I would normally expect to check the value of the argument string as the first statement within main, but then the whole purpose of this exercise is to demonstrate exception handling..!
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stuart Lord:
... got rid of the error message. ...



I work almost exclusively by the cut/copy/paste/blunder/groan approach. This is called cut and try in traditional engineering, I am sure the Object Oriented camp calls this horrible and unspeakable.

Consequent to my approach, I have learned to to tediously follow the code execution path from the very first point one can trace it back to and walk, step-by-step, through every statement and all operators in each statement.

Just because you got rid of the error message does not mean that you fixed the problem. There seems to be plenty of discussion, but obvious to me is the necessity of checking before trying to assign it or do something with

Failure to do this passes the Exception handling beyond the scope of your code, ouch.
[ April 29, 2007: Message edited by: Nicholas Jordan ]
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am afraid you are getting all confused about which Exception you get.

If you don't pass a Command Line argument, you will get an ArrayIndexOutOfBoundsException, thrown in the main method when you try to handle args[0].

You can't get an Exception from the reverseString method, however hard you try, because you have written the "for" loop correctly. If you pass in a zero-length String, you get a zero-length String back, with all the letters it hasn't got in reverse order! I tried it:and I got this output, where "" means sending an empty String as a command line argument.


[Campbell@dhcppc0 disk-1]$ java trivia.StringReverserTest
Exception occurred: 0
[Campbell@dhcppc0 disk-1]$ java trivia.StringReverserTest ""
"" reversed is ""

[Campbell@dhcppc0 disk-1]$ java trivia.StringReverserTest Campbell
"Campbell" reversed is "llebpmaC"

[Campbell@dhcppc0 disk-1]$ java trivia.StringReverserTest Campbell Ritchie
"Campbell" reversed is "llebpmaC"

[Campbell@dhcppc0 disk-1]$ java trivia.StringReverserTest "Campbell Ritchie"
"Campbell Ritchie" reversed is "eihctiR llebpmaC"

Note the difference between Campbell Ritchie and "Campbell Ritchie".

CR
[ April 30, 2007: Message edited by: Campbell Ritchie ]
reply
    Bookmark Topic Watch Topic
  • New Topic