Results 1 to 7 of 7
Thread: missing return statement
- 04-26-2010, 02:08 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 55
- Rep Power
- 0
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.
- 04-26-2010, 02:11 PM #2
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? :)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); } } }Carpe Diem
Each day's a gift and not a given right
- 04-26-2010, 02:17 PM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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:
would cause a missing return statement error, while this:Java Code:public boolean lessThanZero(int i) { if(i < 0) return true; if(i >= 0) return false; }
would be fine, since the second return statement occurs if the if statement evals to false.Java Code:public boolean lessThanZero(int i) { if(i < 0) return true; return false; }
- 04-26-2010, 02:22 PM #4
Member
- Join Date
- Apr 2010
- Posts
- 55
- Rep Power
- 0
Hmm im not quite sure how to exit the loop after the first operator is reached.
Maybe recursion works?
Thanks anyway appreciate it :D
- 04-26-2010, 02:27 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 55
- Rep Power
- 0
Is there any way to force an exit from a loop btw?
- 04-26-2010, 02:36 PM #6
Member
- Join Date
- Apr 2010
- Posts
- 55
- Rep Power
- 0
Problem solved thanks all for your help :)
-Bayan-
- 04-26-2010, 03:15 PM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Similar Threads
-
Missing Return Statement Error
By darkblue24 in forum New To JavaReplies: 13Last Post: 02-16-2010, 08:22 PM -
giving missing return statement error.due to withdraw method.
By qadeer37 in forum New To JavaReplies: 5Last Post: 01-16-2010, 11:14 PM -
Missing return statement help and format method help
By Chewart in forum New To JavaReplies: 18Last Post: 12-02-2009, 12:01 PM -
Missing Return Statement error
By anilanar in forum New To JavaReplies: 2Last Post: 08-20-2009, 01:02 AM -
there is no return statement
By gabriel in forum New To JavaReplies: 17Last Post: 12-03-2008, 04:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks