Ask coding questions

← Back to all posts
I'm stuck...
OtakuMom

I've been working on this for about two weeks, but I don't know why I'm getting this error.

Here is the code: https://repl.it/@OtakuMom/NHTIPeople

First it tells me that I need a return type, but when I put it in it gives me an illegal start of type error, so I am just thoroughly confused. I even tried to delete what was there and reenter it, but it didn't help. Any help is appreciated!

Voters
ObaidBari
Jennyr562
OtakuMom
Comments
hotnewtop
Jennyr562

Task 1. Modify the function count_down()

def count_down(second):
out = None
if second = 0
out = "0\nIgnition\nLift off"
elif seconds = 5
out str(second) + 'Dragon has cleared the tower'
elif seond > 0 and second < 5:
out = str(second)
elif second == -15 or second == -30 or second == -60
out = 'T-' + str(second -1) + 'seconds'
elif second < 10:
out = None
elif second >= 10:
out str(second
-1)
return out

Special Decision to Determine if this code should run

if name == "main":

Task 2. Take input for seconds variable

seconds = int(input())

Convert Positive Seconds to Negative Seconds at Start

if seconds > 0:
seconds = seconds * -1

Task 3. Modify Loop and Call Count Down Function

for s in range(second , 6):
if count_down(s) != None:
print(count_down(s))

SixBeeps

Let's look at the function in question:

If we just look at the if statement, if the answer is not 1, then it returns answer. But, if it is 1, then it calls s.people() without ever returning anything. Try putting a return statement directly after the conditional and see if that works.

OtakuMom

@niorg2606 I just tried putting a return statement, but now it's telling me "illegal start of type" and "<identifier> expected." I feel like the more I try to correct it the more problems I run into trying to fix it.

MatthewDoan1

@OtakuMom

The problem is with where you placed your return statement.

Here's a revised version of your code.

Do you see the difference? In your code, you placed the return answer; line outside the method; in my revised code, not only did I place it inside the method, but since you're returning the answer in both cases, you don't need to write return answer; twice.

OtakuMom

@MatthewDoan1 I do see the difference, but I'm still getting frustrated. I took it out, so now it's telling me I need a return statement there. And when I put the return statment there it tells me that it's an eror, as well, so I'm just on an endless loop at this point.

MatthewDoan1

@OtakuMom Whoops, my bad; there was a typo in my code. Try this: it should be fixed now!

I made a fork of your repl here, if you'd like to take a look at it. I fixed the error from NhtiPeopleConsoleUi.java, but there are compilation errors in Student.java. This is because in the getStudents() method, you have multiple return statements, one after the other.

Now, return is supposed to be the last line in a method body. If you have a void method, then you don't need to return anything - or just have an empty return, like so:

However, if you are returning something, like in your code:

then you must have a return value. I see that you have return id, which is somewhat correct. However, the problem is when you put another return after the return id. The return keyword is supposed to give back a value to the caller; it's supposed to be the end of the method. Anything after the return won't be executed. This is why you are getting a compiler error; you shouldn't have any code after a return.

But wait, you might say! What about this code:

What about the rand() method? Why is there a return after the return? Well, you might notice that the first return is the result of an if-statement. The Java compiler is smart enough to realize that the if-statement might not be executed, and the method needs to return a value. So you need multiple returns in that method to ensure that the method will give back a value consistently.

If you have any questions, feel free to ask!

  • Matthew
MatthewDoan1

@OtakuMom Have you tried this solution?