Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By dlorde

Thread: Looping issue

  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default Looping issue

    I am very aware of how loops should behave. If I have:

    Java Code:
    for(int i=0; i<4; i++){
    System.out.println("*");
    }
    I get:
    *
    *
    *
    *

    Simple enough...

    However, I am seeing weird behavior in my code:
    Java Code:
      System.out.println("should loop "+data.length+" times");
                        for (int j = 0; j < data.length; i++) {
                            System.out.println(i+" of "+data.length);
                            data[i] = Integer.parseInt(temp[i]);
                            System.out.println("adding a number");
                        }
    I get this output
    Java Code:
    should loop 4 times
    0 of 4
    adding a number
    1 of 4
    adding a number
    2 of 4
    adding a number
    3 of 4
    adding a number
    4 of 4
    ErrorEncountered: 4
    Why is it going to the 5th iteration? Maybe I'm overlooking something simple.
    Last edited by Fubarable; 07-11-2011 at 04:50 AM. Reason: quote tags changed to code tags

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    amazing how reading through your own post helps...figured it out.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Yeah, i and j thing.

    Also note that I changed your quote tags into code tags in your second block of code, to help make it more readable.

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    Quote Originally Posted by sehudson View Post
    amazing how reading through your own post helps...figured it out.
    It's sometimes known as 'The Cardboard Guru' because, so often, the simple act of trying to explain a tricky bug to someone is enough for you to spot the problem yourself - so you might as well have been explaining to a piece of cardboard...
    Fubarable likes this.

Similar Threads

  1. help with program!! looping issue?
    By zombian in forum New To Java
    Replies: 6
    Last Post: 11-21-2010, 10:14 PM
  2. Looping
    By Dean29126 in forum New To Java
    Replies: 3
    Last Post: 09-08-2010, 02:01 PM
  3. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM
  4. Looping Help Please
    By JonnySnip3r in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 05:57 AM
  5. Class Scanner looping issue
    By Stev0 in forum New To Java
    Replies: 1
    Last Post: 05-25-2008, 06:53 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
  •