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 ^^

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;
    
    }

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

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

oh thanks :D

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..

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

Edited 3 Years Ago by Nick Evan: Fixed formatting

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.