Results 1 to 6 of 6
  1. #1
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default find first empty cell?

    What i want to do is use a for-loop to find the first empty cell in a 2D array and put a value in it. However, my code applies the loop to all empty cells in the array. What am i doing wrong here?
    Java Code:
    for (int row = 0; row < GRIDSIZE; row++) {
                    for (int col = 0; col < GRIDSIZE; col++) {
                        if (isEmpty(row, col) == false) {
                            printIt(); 
                        } else {
                            setCell(row, col, 1);

    Any help would be appreciated.

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

    Default

    Break out of the loop when you've found an empty cell (and set a value in it):

    Java Code:
    } else {
       setCell(row, col, 1);
       return; // or break;
    }
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    it still sets the value of more than one empty square. any other ideas?
    edit: i got it to work. thanks anyway
    Last edited by truant420; 03-15-2011 at 09:21 AM.

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

    Default

    Quote Originally Posted by truant420 View Post
    it still sets the value of more than one empty square. any other ideas?
    edit: i got it to work. thanks anyway
    I guess you simple used a break to get out of the (innermost) loop; you need to break out of two loops.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    yeah, that was the case. thank you, Jos.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

Similar Threads

  1. How to find out if part of an array is empty
    By counterfox in forum New To Java
    Replies: 2
    Last Post: 10-13-2010, 06:09 PM
  2. Empty Form?
    By Vorfin in forum New To Java
    Replies: 13
    Last Post: 07-04-2010, 11:54 PM
  3. Why is this stakc empty?
    By jigglywiggly in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 06:22 AM
  4. JXL Empty cell problem
    By ironicending in forum New To Java
    Replies: 1
    Last Post: 01-26-2009, 03:32 PM
  5. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 08:36 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
  •