Results 1 to 8 of 8
  1. #1
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default Simple code not working

    Hello everyone. I can't figure out what is wrong with my code. It seems really dumb.
    Java Code:
            int i = 0;
            int stupid_int_cuz_java_is_being_dumb = 0;
            while(i < array.length)
            {
                while(i < array.length && array[i] != true)
                {
                    i++;
                }
                System.out.println("I " +i);
                if(i > (i - stupid_int_cuz_java_is_being_dumb))
                {
                    stupid_int_cuz_java_is_being_dumb = i;
                }   
                System.out.println(stupid_int_cuz_java_is_being_dumb);
                i++;
            }
    For some reason I can't get 'stupid_int_cuz_java_is_being_dumb' to = 10! 'i' always prints out 10! Please help :)
    Last edited by davetheant; 02-04-2011 at 12:28 AM. Reason: Unclear

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    This makes no sense at all. Can you please explain what you are trying to do. Also write a SSCCE using meaningful names. I have no idea what stupid_int_blahblahblah is supposed to represent.

  3. #3
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    Oh sorry I forgot that I had 'array' there. Array is a one-dimensional array of booleans (they are all false to begin with). I am trying to return an array that is identical, except that it changes the middle of the longest sequence of unoccupied places to true. So it should change the 5th element to true.

  4. #4
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by davetheant View Post
    Array is a one-dimensional array of booleans (they are all false to begin with).
    What? If you replaced "array" with "stupid_int_cuz_java_is_being_dumb", then it is an int, not an array of booleans.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java Code:
    if(i > (i - stupid_int_cuz_java_is_being_dumb))
    This will only be false once when blahblahblah is zero so why not simply the code to if (blahblahblah != 0)

    But I still maintain that none of this makes sense even after your explanation.

  6. #6
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    while(i < array.length)
    while(i < array.length && array[i] != true)

    array is a one-dimensional array of booleans.

  7. #7
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    Can we delete this thread? I figured it out

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    I've taken a closer look at your code.

    > i is 0
    > stupid is 0
    > i is less than 10 (I'm assuming that is the length of your boolean array) so outer while condition is true
    > i is still less than 10 and boolean at array[0] is false so inner while condition is true
    > i is incremented to 10 when it exits due to i not being less than 10 anymore.
    > print I 10
    > As mentioned above the if statement is false because 10 is not greater than 10 - 0
    > print 0
    > increment i to 11
    > 11 is not less than 10 outer while condition fails
    > Done

    Once again your code does not make sense.

Similar Threads

  1. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  2. Why is the code not working
    By sanox in forum New To Java
    Replies: 12
    Last Post: 09-08-2009, 11:28 AM
  3. Simple TCP chat application not working for me
    By sundarrajan in forum New To Java
    Replies: 0
    Last Post: 08-13-2009, 08:31 AM
  4. JNI: Why this code not working?
    By playwin2 in forum Advanced Java
    Replies: 5
    Last Post: 11-25-2008, 01:12 PM
  5. simple validations not working
    By rahaman.athiq in forum Web Frameworks
    Replies: 0
    Last Post: 11-06-2008, 06:10 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
  •