0

i have this code, i need to have for() inside this boolean method,
but it shows an error that return method is missing.
how can i solve it?

public static boolean findPrevious(int dataIdx, int featIdx, String data, String[][] array){
        for(int i=0; i<dataIdx; i++)
        {
            if(array[i][featIdx] == data)
                return false;
            else
                return true;
        }    
    }

thanks ^^

3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by Ezzaral
Featured Replies
  • 1
    Ezzaral 2,714   5 Years Ago

    You can add a [ICODE]break[/ICODE] statement after [ICODE]found=true[/ICODE] as well. There is no reason to keep looping once found. Read More

0

aha! found a way :D
posted it, so anyone can find it useful :)

public static boolean findPrevious(int dataIdx, int featIdx, String data, String[][] array){
        
        boolean found = false;
        
            for(int i=0; i<dataIdx; i++){
                if(array[i][featIdx] == data)
                    found = true;
            }
        
        return found;
    
    }
1

You can add a break statement after found=true as well. There is no reason to keep looping once found.

0

You can add a break statement after found=true as well. There is no reason to keep looping once found.

oh thanks :D

0

i have this code, i need to have for() inside this boolean method,
but it shows an error that return method is missing.
how can i solve it?

public static boolean findPrevious(int dataIdx, int featIdx, String data, String[][] array){
        for(int i=0; i<dataIdx; i++)
        {
            if(array[i][featIdx] == data)
                return false;
            else
                return true;
        }    
    }

thanks ^^

Hey.. ur code has logical errors...
just return the boolean value after the for loop

public static boolean findPrevious(int dataIdx, int featIdx, String data, String[][] array){

boolean flag=true;

for(int i=0; i<dataIdx; i++)
{
for(int i=0; i<dataIdx; i++)
{
if(array[featIdx] == data)
return false; //break statement cannot be used after returning a value.

}
return flag;
}
}

Read some SCJP books and try to solve some logical questions...

I assure you that u will become confident in Core java after reading that books

I can also send u some scjp soft copies if you would like..

0

wow really? thanks :)
u r so kind :icon_cheesygrin: i would love to have them
my email is [snipped]

Edited by Nick Evan: Fixed formatting

0

harinath, obvious you did not read the rest of the thread. She fixed the error.

Your own code has logical errors - it's returning the wrong boolean value and it's looping twice over the same range.

As far as the return within the loop, that is just a choice. It can be done there or at the end. Either is valid. Personally I would return directly from within the loop, but some people prefer to have a single return point at the end.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.