Jump to content

Java: Error-"This method must return a result of type Boolean"

RockSolid1106
Go to solution Solved by minibois,

A function with a return type (in this your function "inarray()") must always be able to return a value (of type boolean, in this case).

 

Dissecting your function, if there is data in the array, the for-loop will run and will spit out a return true or return false.

What if there is no data in the array though? That would mean the function doesn't ever reach the 'return true;' or 'return false;' parts of the code.

That is what the code is complaining about.

 

You would fix that by simply doing this:

public static Boolean inarray(String array[], String needle){
        for(int i = 0; i < array.length; i++){
            if(array[i]==needle){
                return true;
            }
        }
	return false;
}

 

I have applied a few changes to your code.

'Boolean p' is set to false. By default, you want to assume the needle-word is not contained in the array, unless proven otherwise. You first set it to false, then loop through the array and if the code finds out the needle-word is there, then it decides to set the boolean to true.

 

I removed the else.

By saying "return false", you end a function. What your current function did, is it looks at the first word in the array and it either sees it's the needle-word or not and returns a true or false value (in your example false, as it compares 'hello' to 'are') and that's it. I assumed you wanted the function to look at the entire array?

With the above changes, it now looks at every word in the array and if one (or multiple) word(s) match the needle-word, it returns true. If it doesn't find the needle-word (doesn't enter the if statement) it will simply loop through the entire array, reach 'return p;' (which would be false, like declared above) and that's it.

 

Now with the above there will always be a return path.

Either it finds the needle-word in the array and returns true, or it doesn't and it returns false.

 

TL;DR: the reason you got an error is because not all code-paths had a return statement attached (in this case the "no content in the array" path).

So I am trying to make a very simple program, which would sift through an array to check if it contains my specified word. If the word is found in the array, a true(boolean) value will be returned, else false. So I have created a method for that and it throws an error:

This method must return a result of type Boolean

My code:

public class pg202Q7 {
    public static void main(String args[]){
        String x[]={"hello", "how", "are", "you"};
        System.out.println(inarray(x, "are"));
        }

    public static Boolean inarray(String array[], String needle){
        int i=0;

        for(;i<array.length; i++){
            if(array[i]==needle){
                return true;
            }
            else{
                return false;
            }
            }
        }
        }
    

 

And here's the full error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        This method must return a result of type Boolean

        at pg202Q7.inarray(pg202Q7.java:7)
        at pg202Q7.main(pg202Q7.java:4)

 

And this shows the line numbers to make it easy to understand:

Spoiler

image.png.2e77a49ed806510d2256b5505d0e859f.png

Thanks!

Edited by RockSolid1106
On 4/5/2024 at 10:13 PM, LAwLz said:

I am getting pretty fucking sick and tired of the "watch something else" responses. It's such a cop out answer because you could say that about basically anything, and it doesn't address the actual complaints. People use it as some kind of card they pull when they can't actually respond to the criticism raised but they still feel like they need to defend some company/person. If you don't like this thread then stop reading it. See how stupid it is? It's basically like telling someone "shut the fuck up". It's not a clever responsive, it doesn't address anything said, and it is rude. 

 ^

 

bruh switch to dark mode its at the bottom of this page

VPN Server Guide

Link to comment
Share on other sites

Link to post
Share on other sites

Basically what it says, you need to return a value at the end of your method. In this particular case, move the "return false" statement to the end of the method.

Java is not C, you don't need to declare all your variables at the top of the method. You can declare and use a variable anywhere in your method, it's like some arcane magic.

Also, use proper formatting to make the life of everyone involved a bit easier (including yours).

Link to comment
Share on other sites

Link to post
Share on other sites

A function with a return type (in this your function "inarray()") must always be able to return a value (of type boolean, in this case).

 

Dissecting your function, if there is data in the array, the for-loop will run and will spit out a return true or return false.

What if there is no data in the array though? That would mean the function doesn't ever reach the 'return true;' or 'return false;' parts of the code.

That is what the code is complaining about.

 

You would fix that by simply doing this:

public static Boolean inarray(String array[], String needle){
        for(int i = 0; i < array.length; i++){
            if(array[i]==needle){
                return true;
            }
        }
	return false;
}

 

I have applied a few changes to your code.

'Boolean p' is set to false. By default, you want to assume the needle-word is not contained in the array, unless proven otherwise. You first set it to false, then loop through the array and if the code finds out the needle-word is there, then it decides to set the boolean to true.

 

I removed the else.

By saying "return false", you end a function. What your current function did, is it looks at the first word in the array and it either sees it's the needle-word or not and returns a true or false value (in your example false, as it compares 'hello' to 'are') and that's it. I assumed you wanted the function to look at the entire array?

With the above changes, it now looks at every word in the array and if one (or multiple) word(s) match the needle-word, it returns true. If it doesn't find the needle-word (doesn't enter the if statement) it will simply loop through the entire array, reach 'return p;' (which would be false, like declared above) and that's it.

 

Now with the above there will always be a return path.

Either it finds the needle-word in the array and returns true, or it doesn't and it returns false.

 

TL;DR: the reason you got an error is because not all code-paths had a return statement attached (in this case the "no content in the array" path).

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

35 minutes ago, minibois said:

I assumed you wanted the function to look at the entire array?

Yes, and the for-loop did that right? So the for loop basically loops through every item in the array and then checks if it matches the needle. If it does, return true, and that's my understanding. You imply that my code only checks the first word, but I beg to differ. 

 

The code works with the changes you and @Trinopoty suggested.

On 4/5/2024 at 10:13 PM, LAwLz said:

I am getting pretty fucking sick and tired of the "watch something else" responses. It's such a cop out answer because you could say that about basically anything, and it doesn't address the actual complaints. People use it as some kind of card they pull when they can't actually respond to the criticism raised but they still feel like they need to defend some company/person. If you don't like this thread then stop reading it. See how stupid it is? It's basically like telling someone "shut the fuck up". It's not a clever responsive, it doesn't address anything said, and it is rude. 

 ^

 

bruh switch to dark mode its at the bottom of this page

VPN Server Guide

Link to comment
Share on other sites

Link to post
Share on other sites

Oh I am extremely sorry, I got what you meant. The loop will immediately exit and return false when it does not see the first word in the array as the needle.

 

EDIT: Marking @minibois's answer as the solution, because it described exactly what the issue was.

Edited by RockSolid1106
On 4/5/2024 at 10:13 PM, LAwLz said:

I am getting pretty fucking sick and tired of the "watch something else" responses. It's such a cop out answer because you could say that about basically anything, and it doesn't address the actual complaints. People use it as some kind of card they pull when they can't actually respond to the criticism raised but they still feel like they need to defend some company/person. If you don't like this thread then stop reading it. See how stupid it is? It's basically like telling someone "shut the fuck up". It's not a clever responsive, it doesn't address anything said, and it is rude. 

 ^

 

bruh switch to dark mode its at the bottom of this page

VPN Server Guide

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, RockSolid1106 said:

Oh I am extremely sorry, I got what you meant. The loop will immediately exit and return false when it does not see the first word in the array as the needle.

Exactly, you got it 😄

The for-loop was the perfect approach to check over all the words in the array. The "return false" in the for-loop just meant it would only check the first word in the array and never the second, third, etc.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

One minor note: You should be using "boolean", not "Boolean" unless you explicitly need to be able to also return null values.

 

"Boolean" is a reference type can can be null, true and false. If you do not initialize it, it defaults to null.

"boolean" is a primitive type and can only be true or false (and defaults to false).

 

Technically someone using your method needs to check for null, because your method could possibly return a null value.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×