简体   繁体   中英

method returning value in Java

what is the wrong with this fragment of code. In eclipse why it is showing that method must return a double value?

public void setLength(double length){
    if(length>0.0 && length<20.00){
        this.length=length;
    }else{
        dataRight=false;
        throw new IllegalArgumentException( "Length is not correct." );
    }
}

public double  getLength(){
    if(dataRight==false){
        System.out.printf("\nAs wrong data, calculation Not possible.\n ");
    }else{
        return length;
    }
}

because you define a result value of type double here:

public double  getLength()
      {
        if(dataRight==false)
        {
         System.out.printf("\nAs wrong data, calculation Not possible.\n ");
        }
        else
        {
          return length;
        }
       }

but in your first if condition you return nothing.

return at least a default value for the first condition or throw an exception if this is absolutly invalid.

if(dataRight==false)
        {
         System.out.printf("\nAs wrong data, calculation Not possible.\n ");
         return -1;
        }

or

public double  getLength() throws Exception
  {
    if(dataRight==false)
    {
     System.out.printf("\nAs wrong data, calculation Not possible.\n ");
     throw new Exception("wrong data, calculation Not possible.");
    }
    else
    {
      return length;
    }
   }
if(dataRight==false)
    {
     System.out.printf("\nAs wrong data, calculation Not possible.\n ");
    // should return from here as well or throw exception 
    }

The error is in the getLenght() method.

If the condition in the if statement is true, you return nothing. Otherwise, you return a double.

So the Java compiler (not Eclipse) is expecting a double to be returned.

You probably want something like

public double getLength() {
    if( dataRight == false )
        throw new RuntimeException("\nAs wrong data, calculation Not possible.\n ");
    return this.length;
}
 public double  getLength()
 {
    if(dataRight==false)
    {
       System.out.printf("\nAs wrong data, calculation Not possible.\n ");
       return 0.0; //<-- Return path must exist for all possible paths in a method, you could also add exception handling
    }
    else
       return length;
 }
public class Test {

    private double length;
    private boolean dataRight = true;

    public void setLength(double length) {
        if (length > 0.0 && length < 20.00) {
            this.length = length;
        } else {
            dataRight = false;
            throw new IllegalArgumentException("Length is not correct.");
        }
    }

    public double getLength() {
        if (!dataRight) {
            System.out.printf("\nAs wrong data, calculation Not possible.\n ");
            return 0.0; // <<<--- If dataRight is false then return double
                        // default values
        }
        return length;
    }

    public static void main(String[] args) {
        Test test= new Test();
        test.setLength(24);
        System.out.println(test.getLength());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM