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!
Your method is declared with an int return type which means that every path through your method must have a return statement:
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.
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?
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.