Ed Taylor Ed Taylor - 1 month ago 7
Java Question

Method that checks the result of another method for an if statement

I need to alter a method in a class:

public void removeFile(int index) {
if(index >= 0 && index < files.size()) {
files.remove(index);
}
}


to use this method:

public boolean validIndex(int index) {
if (index < 0 || index > files.size() - 1) {
return false;
} else {
return true;

}
}


instead of this part of the first method:

if(index >= 0 && index < files.size())


The aim is that the first method checks whether the second method returns as true before it removes the file.

Answer

Is this what you're looking for?:

public void removeFile(int index)
    {
        if(validIndex(index))
        {
            files.remove(index);
        }

    }




   public boolean validIndex(int index)
   {
       return (index >= 0 && index < files.size() - 1);
   }