Search code examples
javaintegerreturnreturn-valuereturn-type

Can anyone help on this This method must return a result of type int


I am stuck in here I want to print these values but it just doesn't allow me. Help please

public int horaPraticada(Intervalo crime) {
    if (inicio != crime.getStart() && fim != crime.getEnd()) {
    
        return  (crime.getEnd() - crime.getStart()); }
        
    if (inicio > crime.getStart() && fim < crime.getEnd()) {
        return  (crime.getEnd() - crime.getStart());
    }
    if (inicio == crime.getStart() && fim > crime.getEnd()) {
        
        return (crime.getEnd() - crime.getStart());
    }
    if (inicio != crime.getStart() && fim == crime.getEnd()) {
        return (crime.getEnd() - crime.getStart());
    
    }
    
    
    
    
}

Solution

  • To further improve GriffeyDog's answer: the compiler does not know, if it is possible or not that all conditions fail. If they would all fail, then the method would not return anything. Therefore, you need to add a return statement outside of the conditions.