Results 1 to 11 of 11
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Help returning value from loop?

    In the following code, I need to return p without instantiating a new product.

    Java Code:
        public Product getProduct(int pid) {
            for (Product p:this) {
                if (p.productID == pid) {
                    return p;
                }
            }
        }

    Is it possible to get around this problem without instantiating a new Product?

    This gives me variable may not have been initialized error:

    Java Code:
        public Product getProduct(int pid) {
            Product found;
            for (Product p:this) {
                if (p.productID == pid) {
                    found = p;
                }
            }
            return found;
        }

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Have you tried making found a public variable?

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

    Default

    Quote Originally Posted by Dark View Post
    Have you tried making found a public variable?
    Wow, that works! Thanks!

    How does that work because if the product doesnt exist found wont be initialised? Which leads me to my next question - what protection should i add in to stop the program from throwing an error if its not initialised?

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

    Default

    Quote Originally Posted by ozzyman View Post
    In the following code, I need to return p without instantiating a new product.

    Java Code:
        public Product getProduct(int pid) {
            for (Product p:this) {
                if (p.productID == pid) {
                    return p;
                }
            }
        }

    Is it possible to get around this problem without instantiating a new Product?

    This gives me variable may not have been initialized error:

    Java Code:
        public Product getProduct(int pid) {
            Product found;
            for (Product p:this) {
                if (p.productID == pid) {
                    found = p;
                }
            }
            return found;
        }
    For the first variant you have to add a 'return null;' statement at the end of your method body, indicating that the wanted Product was not in the Collection; or you have to initialize local variable 'found' to null to keep the compiler's mouth shut. It doesn't matter which of the two variants you use (I prefer the first variant because it doesn't need a local variable but that's just me ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    The problem is this: what happens if you have no Products in "this"? Or if the pid is not found in any of the Products?

    In that case, your "found" variable will not be initialized to anything. Java doesn't know what to do with that. What would you like to return in that case?

    Edit- Too slow. But I can almost guarantee that "adding a public variable" is not at all what you should be doing. All that's doing is initializing it to null by default. Class variables are automatically initialized to a default value (in the case of Objects, it's null). Local variables are not.
    Last edited by KevinWorkman; 04-11-2011 at 12:17 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Initialize it to null, like JosAh stated. That way whenever it gets call it will already be null, you'll never have an empty variable getting returned.

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

    Default

    Okay I get it now, thanks to all of you :). I took JosAhs variant because i'd rather not have the public variable either, so this is my working method:

    Java Code:
        public Product getProduct(int pid) {
            Product found = null;
            for (Product p:this) {
                if (p.productID == pid) {
                    found = p;
                }
            }
            return found;
        }
    At the other end i'll just check that the value returned is not null before I work on it.

    Thanks again!

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I think his variant was getting rid of the local, unless I misread his post.

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

    Default

    oh yes you're right. it should have been:

    Java Code:
        public Product getProduct(int pid) {
            for (Product p:this) {
                if (p.productID == pid) {
                    return p;
                }
            }
            return null;
        }

    even better! :)

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    Quote Originally Posted by Dark View Post
    I think his variant was getting rid of the local, unless I misread his post.
    You misread his post. Adding a global variable only worked because it was automatically initialized to null. There's really no point to doing that instead of simply initializing the local variable to null or returning null at the end of the method.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

Similar Threads

  1. help returning a min value
    By Meta in forum New To Java
    Replies: 7
    Last Post: 04-11-2011, 12:28 AM
  2. Help with returning
    By Da1dmoney in forum Eclipse
    Replies: 3
    Last Post: 12-15-2010, 02:38 AM
  3. modulus value keeps returning a zero value
    By leoshiner in forum New To Java
    Replies: 3
    Last Post: 10-24-2010, 04:11 PM
  4. Keeps returning null
    By ribbs2521 in forum New To Java
    Replies: 7
    Last Post: 02-23-2009, 02:25 AM
  5. returning arrays
    By cjohnson412 in forum New To Java
    Replies: 4
    Last Post: 11-25-2008, 01:30 PM

Posting Permissions

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