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 14 of 14

Thread: Having some nested loop problem

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having some nested loop problem

    Having trouble in result wont displaying pattern II

    Please help to check where is my error in this code for nested loop displaying two number pattern as below .



    i've the same code like this for

    Pattern I

    1 2 3 4 5 6

    1 2 3 4 5

    1 2 3 4

    1 2 3

    1 2

    1

    Pattern II

    123456
    12345
    1234
    123
    12
    1 (P/s pattern 2 is having space infront and align at behind )


       String output = "";
     
          nextRow:  
             for ( int row = 6; row >= 1; row-- ) {
                output += "\n";
     
                for ( int column = 1; column <= 10; column++ ) {
     
                   if ( column > row )
                      continue nextRow; 
                   output += column ;
                }
             }
          nextnextRow:  
             for ( int row = 6; row >= 1; row-- ) {
                output += "\n";
     
             for (int space = 6; space > row; space-- ) {
     
                output += "\t";
                for (int column = 1; column <= row; column++ ) {
     
                   if (column > space)
                      continue nextnextRow; 
                   output += column ;
                }
             }
     
          JOptionPane.showMessageDialog(
             null, output,"Result",
             JOptionPane.INFORMATION_MESSAGE );
          System.exit( 0 );
       }
    }
    }
    Last edited by joel1314; May 24th, 2012 at 06:17 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Having some nested loop problem

    Not really sure what your question is. Does your code do what you want? If not, what does it do instead? Where is your SSCCE? Have you stepped through this with a debugger, or at least added some print statements, to figure out the flow of the program versus what you expect it to do?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having some nested loop problem

    Hmm , the assignment require me to display number in this pattern

    pattern 1

    123456
    12345
    1234
    123
    12
    1

    pattern 2
    123456
    ..12345
    ...1234
    .....123
    .......12
    ........1 (Replace dot with spacebar because it will become like pattern 1 if i simply put spacebar infront of the number


    and the result of my coding shows only pattern 1 , pattern 2 is blank / skipped

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having some nested loop problem

    i could get the result i want using same calculation in system.out.print
    but the assignment require us to use joptionpane with countinue label nested for loop, so i dont know which part of my above code is wrong and made the result for pattern 2 is not displaying

     (int row = 6; row >= 1; row-- ){
    for ( int column = 1; column <= row; column++ )
    System.out.print(column + "" );
    System.out.println();
    }
    System.out.println();
    System.out.println();
     
     
     
    for (int row = 6; row >= 1; row-- ){
    for (int space = 6; space > row; space-- )
    System.out.print( " " );
    for (int column = 1; column <= row; column++ )
    System.out.print( column + "" );
    System.out.println();

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Having some nested loop problem

    I don't see any code that shows a JOptionPane, so I'm not sure where you're stuck.

    Also, I highly recommend surrounding blocks with {}, even if it's just a single line. I'm not sure if your code is working as intended, or if you're missing the brackets.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having some nested loop problem

    hmm , this part is not showing in joptionpane output dialog

      nextnextRow:  
             for ( int row = 6; row >= 1; row-- ) {
                output += "\n";
     
             for (int space = 6; space > row; space-- ) {
     
                output += "\t";
                for (int column = 1; column <= row; column++ ) {
     
                   if (column > space)
                      continue nextnextRow; 
                   output += column ;
                }
             }
     
          JOptionPane.showMessageDialog(
             null, output,"Result",
             JOptionPane.INFORMATION_MESSAGE );
          System.exit( 0 );
       }
    }

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Having some nested loop problem

    What does it show instead?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having some nested loop problem

    Quote Originally Posted by KevinWorkman View Post
    What does it show instead?
    blank , it just show pattern I and skipped pattern II

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Having some nested loop problem

    If you provide an SSCCE, it will be much easier to help you. Until then, I recommend stepping through this with a debugger to figure out what's going on. Recommended reading: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having some nested loop problem

    hmm, i did some correction on the code , now it showing Pattern 1..while the calculation is for pattern 2 , i think it skipped " for (int space = 6; space>=row; space-- ) { output += "\t" ; "
    this part?


     { nextnextRow:
     
             for ( int row = 6; row >= 1; row-- ) {
                output += "\n";
     
                  for (int space = 6; space>=row; space-- ) {
     
                     output += "\t" ;
     
                for (int column = 1; column <= 10; column++ ) {
     
                      if ( column > row )
                      continue nextnextRow; 
     
                          output += column ;

  11. #11
    Junior Member
    Join Date
    May 2012
    Location
    Mumbai, India
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having some nested loop problem

    i don't think it has skipped that statement.
    It enters the tabbed space("\t") after every newline character("\n") because of the '>=' in the condition space >= row.

    You could change the condition for that loop to this -> [for(int space = 0; space < 6-row; space++) ].

    That should do it, i suppose, I haven't tried running it though.

  12. #12
    Junior Member
    Join Date
    May 2012
    Location
    Mumbai, India
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having some nested loop problem

    Also, the loop containing variable column should NOT be a second-level nest.
    It has to be nested only in the loop containing variable row.
    And also there's no need for the label nextnextRow. Just a plain continue statement is enough.(makes the code simpler)

    Just these changes(one about the condition that i mentioned, other about the nesting and last one about the label), and its done!

  13. #13
    Junior Member
    Join Date
    May 2012
    Location
    Mumbai, India
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having some nested loop problem

    There's one more thing, the dialog boxes don't seem to include the tabbed spaces("\t") in the ouput.
    So, changing the statement from [output += "\t";] to [output += " ";] (that include two spaces) displays the second pattern as it should.

  14. #14
    Banned
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having some nested loop problem

    u can use float

Similar Threads

  1. Nested for loop
    By wolf_fcknHaley33 in forum Loops & Control Statements
    Replies: 2
    Last Post: May 23rd, 2012, 08:49 AM
  2. Nid help in nested loop !!
    By Spice in forum Loops & Control Statements
    Replies: 1
    Last Post: February 14th, 2012, 11:10 AM
  3. a problem with a code... (for loop and an "if" nested..)
    By kobi1988 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 13th, 2011, 12:59 PM
  4. nested loop structure
    By captianjaax in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 21st, 2011, 04:00 PM
  5. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM