Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: nested for loop

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default nested for loop

    good morning, i've been stuck on this issue for about 2 weeks now! :-(
    i have a nested loop.
    for(i= 0; i<= 3;  i++)
    {
          for(j= 0; j<= 8; j++)
          {
                System.out.println(i+ "   " + j);
          }
    }

    i know this has an output 01 0,2 0,3, 04 0,5 0,6 0,7 0,8 1,0......1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8 2,0...ETC

    what i want to do is if i set the second for loop variable j to take an inputted integer from the user (say 4) how do i reset it to 0 once it hits 8.

    the problem i keep having is that it goes back and continues the loop from 4 when i want it to reset to 0.

    Thanks!


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: nested for loop

    I don't see anywhere in your code where you attempt to reset your variable. Also I do not fully understand what you are trying to make the code do, please try to explain again

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nested for loop

    Quote Originally Posted by jps View Post
    I don't see anywhere in your code where you attempt to reset your variable. Also I do not fully understand what you are trying to make the code do, please try to explain again

    it was 1am when i wrote that lol, let me try this again.
    i want two loops as you see in the code to do exactly what it's doing.
    00
    01
    02
    03
    10
    11
    12
    13
    20
    21
    22
    23
    etc.

    but when i put in variable at j say 4, then the whole loop will start at 4 not 0. when i try to reset the variable, say inside the last loop at j= 0, i the whole loop just doesn't start, no output. What i want the code to do is that it resets at 0 regardless at where i start. so if i put say 2, then it will go from 2 to 8, then back to 0 while adding 1 to i.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: nested for loop

    i want two loops as you see in the code to do exactly what it's doing.
    If so, what problems are you having with the program?

    but when i put in variable at j say 4, then the whole loop will start at 4
    If you really want some different output, can you post what you want the output to look like?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: nested for loop

    Quote Originally Posted by tardis_ View Post
    i want two loops as you see in the code to do exactly what it's doing.


    Quote Originally Posted by tardis_ View Post
    but when i put in variable at j say 4, then the whole loop will start at 4 not 0.
    when i try to reset the variable,
    How do you change j to 4? How do you reset the variable? Variables i and j are both locally declared, and the control for both loops are hard coded. If you want to vary the number of times a loop runs you will have to use a variable in place of the hard coded numerical values.
    for( int i = iStartValue; i < iStopValue; i++){
       for( int j = jStartValue; jStopValue < 8; j++) {
          //tasks
       }
    }
    I included variables in your code in place of hard coded values. Using variables in these 4 locations gives you the control of the loops you seem to be wanting. Modify the values of the variables outside the loop before the loop(s) start, and inside the loop(s) as the code executes, to suit your needs.
    Last edited by jps; April 18th, 2013 at 01:12 PM. Reason: fixed code tags

Similar Threads

  1. Need help with this nested loop
    By beerye28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 22nd, 2013, 09:51 PM
  2. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 20th, 2012, 03:49 PM
  3. Nested For Loop!
    By samadniz in forum Object Oriented Programming
    Replies: 3
    Last Post: September 3rd, 2012, 04:03 PM
  4. Nested for loop
    By wolf_fcknHaley33 in forum Loops & Control Statements
    Replies: 2
    Last Post: May 23rd, 2012, 08:49 AM
  5. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM