Results 1 to 7 of 7
- 03-14-2011, 08:11 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
Noob question about using ints in booleans
Okay. I know this is pretty basic stuff. Sorry. This is a trouble I have that I don't know how to deal with. I create a boolean if statement, and at some times it won't let my use int's. Of course I am using ints, but I get an error message "this method must return a type of int" because I created an if statement with booleans... but the return is a number, not a true/false. I just don't see what I'm doing wrong. Sample code:
/* calculates the total amount of time required to cook */
private int totalCookTime(int cookingLength, int foodAmount, int stoves, int cookingLengthMinutes) {
if (cookingLength > 0 && cookingLengthMinutes <= 0) {
return ((cookingLength*foodAmount)*MINUTES_IN_HOUR)/stoves;
} else if (cookingLength > 0 && cookingLengthMinutes > 0){
return (((cookingLength*foodAmount)*MINUTES_IN_HOUR)+cook ingLengthMinutes)/stoves;
} else if (cookingLength <= 0 && cookingLengthMinutes > 0) {
return (cookingLengthMinutes*foodAmount)/stoves;
}
}
of course in the body I have initiated all the parametres etc.
So sorry that this question is so dumb.
Oh, and I use Eclipse, if that makes a difference.
Thanks for your time.
Jo
- 03-14-2011, 08:14 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Sorry, your method does not have a return in full path.
- 03-14-2011, 08:15 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Those if statements look fine, are you getting errors? If so, what are they?(don't paraphrase, copy and post the exact error)
Also, use code tags
[c ode]
your code
[/code]
Also, if none of the conditions are met it wont return anything.
- 03-14-2011, 08:16 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Suppose all of your if-tests fail; the compiler doesn't evaluate your code so that could happen. Your method doesn't return anything so your compiler starts to whine about it; the remedy is simple: stick a "return 0;" at the very end of that method even if it is never executed, the compiler can't tell.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-14-2011, 08:22 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Your problem is that all of your "returns" are inside of if blocks. The compiler does not even attempt to evaluate those conditions and so it envisions a possibility that none of those if blocks will evaluate as true and, therefore, your method could wind up not returning anything. Either add an else block that returns a default value or add a return at the end of the method that returns a default value, or better yet, rewrite the method in the following manner
Edit Man, am I SLOW!Java Code:public int someMethod(int arg) { int retVal = someDefaultValue; if (arg > someValue) { retVal = someArgExpression; } else if (arg < someOtherVale) { retVal = someOtherArgExpression; } return retVal; }
- 03-14-2011, 08:23 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
Oh! Thank you very much. When I added the final else statement (which is something that should never happen in the program) it all worked fine. So the error "this method must return a type of int" put me off because I was trying to work out why it wasn't an int!
Thankyou very much. :)
-
This code is pretty messy...
Firstly, your calculation is the same for the first 2 branches and it only changes in the final branch because if cookinglength <= 0 it renders the major part of your calculation as 0.Java Code:private int totalCookTime(int cookingLength, int foodAmount, int stoves, int cookingLengthMinutes) { if (cookingLength > 0 && cookingLengthMinutes <= 0) { return ((cookingLength*foodAmount)*MINUTES_IN_HOUR)/stoves; } else if (cookingLength > 0 && cookingLengthMinutes > 0){ return (((cookingLength*foodAmount)*MINUTES_IN_HOUR)+cook ingLengthMinutes)/stoves; } else if (cookingLength <= 0 && cookingLengthMinutes > 0) { return (cookingLengthMinutes*foodAmount)/stoves; } }
What you have written has the same effect as:
Java Code:int cookingTime = 0; if (cookingLength <= 0 && cookingLengthMinutes > 0) { cookingTime = (cookingLengthMinutes*foodAmount)/stoves; } else { cookingTime = (cookingLength*MINUTES_IN_HOUR*foodAmount + cookingLengthMinutes)/stoves; } return cookingTime;
And even then, it appears to me that your variable cookingLengthMinutes indicates the same value as cookingLength*MINUTES_IN_HOUR ? That is, if cookingLength holds the cooking time in Hours and MINUTES_IN_HOUR = 60. In which case, you don't need any conditional statement because the calculation is the same for all values.
EDIT: Looking at your code again, it seems like you are storing the cooking time in two variables: one for hours and another for minutes e.g.
Total cooking time could be 1hr + 30minutes. But it should be all stored in minutes to simplify calculations and can still be output as hours & minutes.
And if that is the case, then:
Java Code:cookingTime = (cookingLength*MINUTES_IN_HOUR*foodAmount + cookingLengthMinutes)/stoves;
Should be
Java Code:cookingTime = (cookingLength*MINUTES_IN_HOUR*foodAmount + cookingLengthMinutes*foodamount)/stoves;
Infact, then your whole method should look like this:
i.e. the total minutes for cooking is cooking hours*minutes in hour + cooking minutesJava Code:private int totalCookTime(int cookingLength, int foodAmount, int stoves, int cookingLengthMinutes) { int cookingTime = 0; cookingTime = (cookingLength*MINUTES_IN_HOUR*foodAmount + cookingLengthMinutes*foodamount)/stoves; return cookingTime; }
and the total cooking time is (the total minutes*foodamount) divided by number of stovesLast edited by ozzyman; 03-14-2011 at 11:28 AM.
Similar Threads
-
(noob question :0) Making point relative to applet location
By crikey in forum New To JavaReplies: 8Last Post: 08-25-2010, 04:22 PM -
Reallyy noob question
By x2robbie2x in forum New To JavaReplies: 3Last Post: 02-18-2010, 04:58 AM -
Noob button question.
By dudejonne in forum Java AppletsReplies: 4Last Post: 10-20-2009, 10:51 PM -
Question - I'm a noob!
By Insaeno in forum New To JavaReplies: 5Last Post: 08-04-2008, 03:20 AM -
Noob question- easy
By mattonitto in forum New To JavaReplies: 7Last Post: 06-13-2008, 12:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks