Results 1 to 7 of 7
  1. #1
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default missing return statement

    Hi guys could you help me out here? I can't tell whats wrong with my code it keeps giving me a missing return statement, } expected.

    Heres the class im having problems with.
    The program is a calculator in which you type in the expression, which is taken as a string and processed.

    public class calculate
    {
    private String expression;
    private double first;
    public calculate(String s)
    {
    expression=s;
    first=number();
    }
    public double number()
    {
    for(int i=1; i<=expression.length();i++)
    {
    String num ="";
    String temp=expression.substring(i-1,i);
    if(temp.compareTo("1")==0 || temp.compareTo("2")==0 || temp.compareTo("3")==0 || temp.compareTo("4")==0 || temp.compareTo("5")==0 ||
    temp.compareTo("6")==0 || temp.compareTo("7")==0 || temp.compareTo("8")==0 || temp.compareTo("9")==0 || temp.compareTo(".")==0)
    {num+=temp;}
    else
    {
    expression=expression.substring(i);
    return Double.parseDouble(num);
    }
    }
    }

    public double evaluate()
    {
    double num1=first;
    String operation=expression.substring(0,1);
    expression=expression.substring(1);
    double num2=number();
    if(operation.compareTo("+")==0)
    return (num1+num2);
    if(operation.compareTo("-")==0)
    return (num1-num2);
    if(operation.compareTo("*")==0)
    return (num1*num2);
    if(operation.compareTo("/")==0)
    return (num1/num2);
    if(operation.compareTo("%")==0)
    return (num1%num2);
    }
    }

    No braces missing. Any help appreciated.

  2. #2
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Default

    Java Code:
    public double number()
    {
    for(int i=1; i<=expression.length();i++)
    {
    String num ="";
    String temp=expression.substring(i-1,i);
    if(temp.compareTo("1")==0 || temp.compareTo("2")==0 || temp.compareTo("3")==0 || temp.compareTo("4")==0 || temp.compareTo("5")==0 ||
    temp.compareTo("6")==0 || temp.compareTo("7")==0 || temp.compareTo("8")==0 || temp.compareTo("9")==0 || temp.compareTo(".")==0)
    {num+=temp;}
    else
    {
    expression=expression.substring(i);
    return Double.parseDouble(num);
    }
    }
    }
    Doesn't seem to return anything if the "if" part is true? You have to return a double in both cases. Or is it just me who fail to see it? :)
    Carpe Diem
    Each day's a gift and not a given right

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    In the evaluate() method, all return statements have an if attached, and none of those ifs have else clauses. What would happen if all ifs return false? Nothing gets returned. Also, even if you construct the if statements to insure at least one of them evaluates to true, the compiler doesn't know that, so even this:
    Java Code:
    public boolean lessThanZero(int i) {
      if(i < 0)
        return true;
      if(i >= 0)
        return false;
    }
    would cause a missing return statement error, while this:
    Java Code:
    public boolean lessThanZero(int i) {
      if(i < 0)
        return true;
      return false;
    }
    would be fine, since the second return statement occurs if the if statement evals to false.

  4. #4
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    Hmm im not quite sure how to exit the loop after the first operator is reached.
    Maybe recursion works?
    Thanks anyway appreciate it :D

  5. #5
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    Is there any way to force an exit from a loop btw?

  6. #6
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    Problem solved thanks all for your help :)

    -Bayan-

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    By the way, all of your operation.compareTo(<whatever>)==0 logic can be replaced by the simpler operation.equals(<whatever>).

    -Gary-

Similar Threads

  1. Missing Return Statement Error
    By darkblue24 in forum New To Java
    Replies: 13
    Last Post: 02-16-2010, 08:22 PM
  2. Replies: 5
    Last Post: 01-16-2010, 11:14 PM
  3. Replies: 18
    Last Post: 12-02-2009, 12:01 PM
  4. Missing Return Statement error
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-20-2009, 01:02 AM
  5. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 04:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •