Results 1 to 7 of 7
  1. #1
    elfdreaming is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    21

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    9

    Default

    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.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default

    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,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    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

    Java Code:
    public int someMethod(int arg) {
        int retVal = someDefaultValue;
        if (arg > someValue) {
            retVal = someArgExpression;
        } else if (arg < someOtherVale) {
            retVal = someOtherArgExpression;
        }
        return retVal;
    }
    Edit Man, am I SLOW!

  6. #6
    elfdreaming is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    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. :)

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    5

    Default

    This code is pretty messy...

    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;
      }
    }
    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.

    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:

    Java 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;
    }
    i.e. the total minutes for cooking is cooking hours*minutes in hour + cooking minutes
    and the total cooking time is (the total minutes*foodamount) divided by number of stoves
    Last edited by ozzyman; 03-14-2011 at 12:28 PM.

Similar Threads

  1. Replies: 8
    Last Post: 08-25-2010, 04:22 PM
  2. Reallyy noob question
    By x2robbie2x in forum New To Java
    Replies: 3
    Last Post: 02-18-2010, 05:58 AM
  3. Noob button question.
    By dudejonne in forum Java Applets
    Replies: 4
    Last Post: 10-20-2009, 10:51 PM
  4. Question - I'm a noob!
    By Insaeno in forum New To Java
    Replies: 5
    Last Post: 08-04-2008, 03:20 AM
  5. Noob question- easy
    By mattonitto in forum New To Java
    Replies: 7
    Last Post: 06-13-2008, 12:26 AM

Posting Permissions

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