Page 3 of 3 FirstFirst 123
Results 41 to 52 of 52

Thread: Method call

  1. #41
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    I have tryed this one, but it don't work
    Java Code:
    
            if(findProduct(id).getID() != id) or (findProduct(id).getQuantity() == 0)

  2. #42
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default

    it don't work
    Please explain. There are many ways for code to not work.

  3. #43
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    I'm trying to return quantity in the item in stock, but when I insert a number which I know not exist then I get a nullpointer

    Java Code:
            if(findProduct(id).getID() != id) or (findProduct(id).getQuantity() == 0)
    
    Here I try to see if the id I insert and the product id is different or product id and its quantity is 0
    If they are different or 0 return 0, if it is a match between product id and the inserted id then return quantity.
    But I have no idea of how to do it correct.
    Any suggestion?

  4. #44
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default

    I get a nullpointer
    You must test for the null return BEFORE you try to use the returned value.
    Get a Product reference from findProduct() and test it for null BEFORE you call the getID() method.

  5. #45
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Could you please tell me if this look correct
    When I use the print out, it print correct number. If write number 2 in the parameter, it print "not a valid number" since there don't exist anything with that number.
    If i insert number 37, it print quantity 0.
    So it seems to work, but when I try to use the return then I don't know which of the return is returned, since they both return 0
    Java Code:
       public int numberInStock(int id)
        {
            if(findProduct(id) == null)
            {
             System.out.println("Not a valid id " + id); 
            }
            else if(findProduct(id).getID() == id)
            {
              // System.out.println("Quantity " + findProduct(id).getQuantity());
               return findProduct(id).getQuantity();
            }else{
                 }
          return 0;   
       }

  6. #46
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    I have tested the code in a debugger an I can see that the correct return is returned. So I think this method work well now.
    Thank for your help again Norman!

  7. #47
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default

    Does it compile and execute and give you the results that you want?

  8. #48
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Yes, it compile and execute and give the result that I want
    does it code look correct? It should be correct now

  9. #49
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default

    Looks can be deceiving. What matters is: Does it work correctly with all possible test data.

  10. #50
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello,
    Could someone tell me how I can call to a method which is a void method in another class?

  11. #51
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    I have done it. Its works

  12. #52
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    I 'm about to write a code to print all details of product below an inserted value
    What is most correct, write a new method to do it or write a method which call another print method that allready exist?

    I can use this method below to print out product below the inserted value
    Java Code:
       public void printProductValue(int value)
       {
           {
            Iterator it = stock.iterator();
            while(it.hasNext())
            {
                Product product = (Product) it.next();
                if(product.getQuantity() < value)
                {
                System.out.println("Product: " + product);
                }
            }
          }
       }
    Last edited by ralf; 07-17-2011 at 10:11 PM.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. call a JSP method
    By sauravsinha in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 09-26-2011, 04:19 PM
  2. How do I call this method
    By africanhacker in forum New To Java
    Replies: 1
    Last Post: 06-29-2011, 04:03 PM
  3. How to call this method?
    By Rockefella in forum New To Java
    Replies: 12
    Last Post: 01-20-2011, 04:32 PM
  4. how to call method?
    By leapinlizard in forum New To Java
    Replies: 9
    Last Post: 04-29-2009, 11:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 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
  •