Results 1 to 8 of 8
  1. #1
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Why is this method return 0

    I have a class with instance variable price.

    I have a method getThePrice() which gets this just fine.

    However I have decided to use another method getThePrice(int weight) and simply pass zero as an argument for those products that need not be weighed. The problem is that the method I have written does not work, I am a loss as to what I have done wrong. Any help would be appreciated:

    This is the method that does not work:

    Java Code:
    public int getThePrice(int weighedObject){
    
    
                 if(weighedObject == 0){
    
                    return price;
    
                 }
                else if(weighedObject > 0){
    
                    return price * weighedObject;
    
                }
    
                 return price;
    
                }
    But when I use this one everything works fine. I am sure it is something wrong with the method I have written:

    Java Code:
    public int getThePrice(){
    
             return price;
    
             }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by africanhacker View Post
    I have a class with instance variable price.

    I have a method getThePrice() which gets this just fine.

    However I have decided to use another method getThePrice(int weight) and simply pass zero as an argument for those products that need not be weighed. The problem is that the method I have written does not work, I am a loss as to what I have done wrong. Any help would be appreciated:

    This is the method that does not work:

    Java Code:
    public int getThePrice(int weighedObject){
    
    
                 if(weighedObject == 0){
    
                    return price;
    
                 }
                else if(weighedObject > 0){
    
                    return price * weighedObject;
    
                }
    
                 return price;
    
                }
    But when I use this one everything works fine. I am sure it is something wrong with the method I have written:

    Java Code:
    public int getThePrice(){
    
             return price;
    
             }
    That snippet of code seems fine; can you show us how you're calling that method and how you set a (non-zero) value to the price member variable?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ioexcept's Avatar
    ioexcept is offline Advocate
    Join Date
    Jun 2011
    Location
    Philadelphia
    Posts
    3
    Rep Power
    0

    Default

    System.out.println("weighedObject Value [" + weighedObject + "]");
    System.out.println("Boolean Comparison [" + (weighedObject == 0) + "]");

  4. #4
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by ioexcept View Post
    System.out.println("weighedObject Value [" + weighedObject + "]");
    System.out.println("Boolean Comparison [" + (weighedObject == 0) + "]");
    ioexcept, I did not get that.
    Last edited by africanhacker; 06-30-2011 at 05:32 PM.

  5. #5
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    That snippet of code seems fine; can you show us how you're calling that method and how you set a (non-zero) value to the price member variable?

    kind regards,

    Jos
    This works:

    Java Code:
    " " + productArray.get(i).getThePrice();
    This does not work:

    Java Code:
    " " + productArray.get(i).getThePrice(25);
    Thanks for taking the time to help guys (or girls)


    .

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    What is returned by the second method? Not 25*price I presume because you wouldn't have asked this question here ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    It returns two possible values. If the argument passed was 0 it should return the base price of the product. If the argument passed was 25 then it would indeed return 25 * price. I don't get why I would not have asked the question here. I am desperately confused as to why something this simple is not working.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by africanhacker View Post
    It returns two possible values. If the argument passed was 0 it should return the base price of the product. If the argument passed was 25 then it would indeed return 25 * price. I don't get why I would not have asked the question here. I am desperately confused as to why something this simple is not working.
    As already suggested: print the price and the weight factor in the body of that method and see if it clarifies things ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. cant return value from a method
    By gedas in forum New To Java
    Replies: 2
    Last Post: 03-23-2011, 07:37 AM
  2. Not able to return the method value
    By dmakshay2002 in forum Advanced Java
    Replies: 11
    Last Post: 05-28-2010, 02:07 PM
  3. Method won't return value
    By footyvino in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 10:49 AM
  4. Return an object to use in another method
    By TidusSolan in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 08:00 PM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:23 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
  •