• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

code doesnt compile, missing return statement

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey y'all!  
I just started learning java and I've written a program that should (in theory) take two numbers and then display all numbers between those two. But every time I try to compile it, the console output reads: "Range.java:32: error: missing return statement".
I just don't know where to include a return statement without messing up everything? If someone here could help me, that would be most appreciated!  

 
Sheriff
Posts: 17297
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Your method is declared with an int return type which means that every path through your method must have a return statement:
 
Saloon Keeper
Posts: 9720
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Welcome to the Ranch!

Your method is declared with an int return type which means that every path through your method must have a return statement:


Or...
If you really don't need to return a value then declare it as returning 'void'.

What is a 'sequence' ?
 
Fynn Lenz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Junilu Lacar wrote:Welcome to the Ranch!

Your method is declared with an int return type which means that every path through your method must have a return statement:


Or...
If you really don't need to return a value then declare it as returning 'void'.

What is a 'sequence' ?



Thanks for the quick answer!
If I change the method printRange to void, I get this error message: "Range.java:11: error: incompatible types: void cannot be converted to int", which confuses me even further. Do you think I need to change the type of "sequence"? It's a row of numbers, so I guess it could be declared as a String, but that doesn't change the error message.
 
Sheriff
Posts: 4006
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:What is a 'sequence' ?


Fynn Lenz wrote:... It's a row of numbers, so I guess it could be declared as a String ...


I think Carey's asking more like what does sequence represent, not what is its structure/format.
 
Carey Brown
Saloon Keeper
Posts: 9720
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sequence:
Is it an: array, List, or String?
of the values of: 'z', or 'i', or something else?
Why would the caller need the sequence?

Your method is named 'print' but if it is returning a sequence that may not be an appropriate name.
 
Fynn Lenz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-
 
Carey Brown
Saloon Keeper
Posts: 9720
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't seem entirely clear on the task that your program is supposed to perform. You have a formula for 'i' for instance yet 'i' is not used anywhere. The caller is expecting a 'sequence' that it will presumably do something further with. In the mean time printing out the sequence seems to be useful for debugging. The printRange() method also has print statements but they appear to be for debugging because they don't contribute to the 'sequence' to be returned. So, I'm going to make a BIG leap here and rip out most of the debugging prints and return an array of ints as the sequence. Is this what you had in mind?
 
Fynn Lenz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Carey Brown
Thanks for your help! You're right, the print statement didn't really have a use.

I changed my code to this:

,which works. At least my prof's grading software accepts it. I'm not really sure about the return(x) there, but if I don't add it, it won't compile.

Thanks for the help!!
 
Carey Brown
Saloon Keeper
Posts: 9720
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, if your caller doesn't need the sequence to be returned then just return 'void' and do away with your 'return(x);'. Likewise, you are still computing 'i' but not doing anything with it. It doesn't hurt but it only adds confusion to the reader.
 
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic