missing return statement java что значит

Java Hungry

Java developers tutorials and coding.

[Solved] Missing return statement in java error

The missing return statement in java error is mostly faced by beginners. You can familiarize yourself with these kinds of common errors by coding simple java projects. Let’s understand the error first. This error occurs at compile-time. The root cause of the error as the name suggests when the return statement is missing in the program.

[Fixed] Missing return statement in java error

1. Method missing the return statement

I am using the HelloWorld java program as an example. If you compile the above code using below command

you will get the missing return statement error. There are two ways to fix it.

1.1 In printHelloWorld() method we have declared return type as String. But if you notice there is no return statement inside the printHelloWorld() method. Just add that as shown below.

Now compile and run your program using commands below

Yahoo!! your problem is resolved.

1.2 The above situation can also be solved by changing the return type to void without providing any return statement.

Now compile and run your program using below commands

Yahoo!! your problem is resolved.

2. Missing return statement error within if / while / for

If you declare return statement inside if / while / for but not at the end of the method containing them, then you will get missing return statement error as shown below.

If you compile the above code then you will get missing return statement error.

Fix it by adding the return statement for the method similar to the one we did in case 1 above i.e adding line return «Alive is Awesome»;

There is a special case, what happens when you provide return statements inside both if/else block, as shown below. What will be the output?

The above code will compile fine and you will get the following output:

That’s all for today. Please mention in comments in case you have any questions related to missing return statement in java error.

Источник

Missing return statement in java

In Java, missing return statement is a common error which occurs if either we forget to add return statement or use in the wrong scenario.

In this article, we will see the different scenarios when missing return statement can occur.

Missing return statement

Here are the few scenarios where we can get Missing return statement error.

Scenario 1

Let’s take an example, where we have a method that returns absolute value of the given positive number. In this example, we computed the absolute value but forgot to return it. When we compile this code, the compiler reports an error. See the example and output.

Читайте также:  серьги во сне к чему снится женщине серебряные серьги

Solution

We can solve it by using two ways, either add return statement in the code or set return type as void in the method signature. See the examples below, wherein the first example we have added the return statement. We have also added another method getAbsolute2() and returned void from it in case we don’t want to return anything from the method.

Scenario 2

This error can also occur if we have used a return statement in if block. This return statement will execute only when if block executes. Since return statement depends on the if block condition, compiler reports an error. We have to either put return statement in else block or to the end of the method to resolve this.

Note: A method that has return type in its signature must have return statement.

Solution

That’s all about Missing return statement in java.

Share this

Cannot find symbol Java

[Solved] Variable might not have been initialized in Java

Author

Related Posts

[Solved] Variable might not have been initialized in Java

Learn about how to solve Variable might not have been initialized in Java.

[Solved] Class names are only accepted if annotation processing is explicitly requested

Learn about how to fix class names are only accepted if annotation processing is explicitly requested in java.

[Solved] Exception in thread “main” java.util.InputMismatchException

Learn about how to fix java.util.InputMismatchException.

Learn about how to fix uses or overrides a deprecated api in java.

[Fixed] java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriver

Table of ContentsProblem: java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriverSolution 1 : Using UCanAccess jarsSolution 2 : Revert Java version to 7 or before In this post, we will see how to resolve java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriver exception in java. Problem: java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriver This exeception comes in Java 8 because sun.jdbc.odbc.jdbcodbcdriver has been removed from JDK and JRE. This class is used […]

Leave a Reply Cancel reply

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.

Источник

java:17: error: missing return statement

I wrote code to get a number from the user, then check whether the number is prime.

It gives me a missing return statement error!

7 Answers 7

Your function currently does not necessarily return anything. This is the case when number is smaller than or equal to 2. You should add a return statement outside of your for loop, so that the function will always return something.

Side note: The logic to decide whether a number is a prime or not is wrong. If the current function was valid, it would return it is prime if the number was odd and it is not prime if the number was even.

Читайте также:  к чему снится одно и тоже место во сне

While the other answers havd correctly pointed out why you are getting the error, I feel obliged to point out that your code (and other answers here) will not correctly indicate whether the number is prime, or not. Try this instead:

If number variable is less or equal then for loop will not run. So return statement in for statement will not work. To fix your compilation error you should add return statement after for block. Your code will look something like this:

the reason your for loop not execute by condition failed, what is the return value. so it is a error. try this..

You should return the else outside the for loop

If the condition inside the if statement is met, then it will return «is prime» and the «not prime» will not be returned. If the if condition is never met in the loop, the «not prime» will be returned.

In java, a method which has defined return type can not end normally with out returning. You must return value from the method with out any implicit/explicit condition. A method like following is ok:

But a method which is returning as following:

will ask for return statement because after the second end condition, it can’t evaluate the conditions other than the run time. For your case how ever, The problem is with the for loop. Whither it can go inside the for loop or not can’t be evaluated at the compile time. So your program must pass the compile time ensuring that even if your for loop wasn’t executed, it does return.

Источник

Compiling Error: Missing return statement [duplicate]

This program plays craps with 3 different methods. I need help in playing craps but i’m required to have these 3 different methods but for some reason every time I compile I am getting this error:

Code:

8 Answers 8

Java must look at all execution paths. What happens if the while loop ends without returning anything? You may be logically preventing that, but the Java compiler won’t do that analysis.

You can add the last return statement after your code like this:

It will make it compile and your code will still work.

you only return statements are inside the body of an if block.

The compiler doesn’t know if any of those if blocks will ever be reached, so it’s giving you an error.

You might want to have a default return statement at the end

Читайте также:  к чему снится раскрытие преступления

I’m assuming that if that default return statement ever actually gets executed, than it’s an error state, and you should react accordingly.

You are missing a return in your while block:

You should look into consistent code formatting. Not only for us, but for yourself as well so when you look at this code after a few weeks you can still read it.

When you have to add return make sure you add return for every possible execution path of add a default return statement. You program is missing both of them

Here is a very stripped down version of your code:

To fix the missing return statement, add a return statement to code3 or code4.

Источник

«Missing return statement» within if / for / while

I have a question regarding return statements used within if() while() or for() statements.

7 Answers 7

But if you write an if / else block and each one of them is having a return in it then the compiler knows that either the if or else will get executed and the method will return a value. So this time the compiler will not force you.

That’s because the function needs to return a value. Imagine what happens if you execute myMethod() and it doesn’t go into if(condition) what would your function return? The compiler needs to know what to return in every possible execution of your function.

Checking Java documentation:

Definition: If a method declaration has a return type then there must be a return statement at the end of the method. If the return statement is not there the missing return statement error is thrown.

This error is also thrown if the method does not have a return type and has not been declared using void (i.e., it was mistakenly omitted).

You can do this to solve your problem:

That is illegal syntax. It is not an optional thing for you to return a variable. You must return a variable of the type you specify in your method.

You are effectively saying: I promise any class can use this method (public) and I promise it will always return a String (String).

Then you are saying IF my condition is true I will return x. Well, that is too bad. There isn’t any IF in your promise. You promised that myMethod will always return a String.

Even if your condition is always true, the compiler has to assume that there is a possibility of it being false. Therefore you always need to put a return at the end of your non-void method outside of any conditions just in case all of your conditions fail.

Источник

Обзорно-познавательный сайт