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

Thread: another for loop question

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry another for loop question

    I thought I knew how loops worked drawing memory until I got this assignment with JOptionPane. Please read the following.

    Use one and only one for loop to print the following pattern in one dialog box. Do not use nested for loops. Use only one for loop, not two or more. Do not use any other kind of loop. Do not use a switch/case statement or if conditions. The same code should work for 7 lines of asterisks or 17 lines of asterisks, simply by changing the number of times the loop executes, from 7 to 17.

    *
    **
    ***
    ****
    *****
    ******
    *******

    This is what I have put down

    String message = "";
    int i;

    for(i = 0; i < 7; i++)
    {
    message += "*";
    JOptionPane.showMessageDialog(null, message);
    }

    putting JOptionPane inside of the loop will get you 7 JOption boxes with the asterisk incrementing each time. If I put it outside of the loop I will get on box, but 7 asterisks in a row. Now adding a \n to the end of the asterisk will give me 7 asterisks in a row of 1 and a column of seven. How in the world do I get this asterisk to increment in one JOption box? Hints, programing logic, sites, anything that can help is much appreciated!

    trial 2.
    String message = "";
    int i;

    for(i = 0; i < 7; i++)
    {
    message += "*";
    }
    JOptionPane.showMessageDialog(null, message);

    you get one box
    *******

    trial 3
    String message = "";
    int i;

    for(i = 0; i < 7; i++)
    {
    message += "*\n";
    }
    JOptionPane.showMessageDialog(null, message);

    *
    *
    *
    *
    *
    *
    *

    I have tried all sorts of things, but when I write out the memory on a piece of paper, I get stuck. If this was command line, this wouldn't be an issue!



    Thank You for your time,


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: another for loop question

    The job is to build a String with lineend characters at the right places to put the number of *s on each line that are needed to make the display.
    Use a counter to determine when a lineend should be added.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: another for loop question

    I am not quite sure if I follow you norm. in this case, i is the counter, using i++ in the for loop will only bump up the count. For a better understanding I showed i in the count of

    message += i + "*\n";

    output
    1*
    2*
    3*
    4*
    5*
    6*
    7*

    I get one JOption box, which is good. not understanding why the for loop won't add the incrementing *. For loop repeats 7 times, each time the loop sees *\n. Okay that is wrong, how do I add in a return, and still get the * to increment without using a nested forloop?

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

    Default Re: another for loop question

    Add a "*" every time around the loop
    Add a "\n" when the counter says to add the next newline.
    To see when to add the newline, take a piece of paper and write the design of *s in the rows they should go in and then count the *'s starting at the beginning:
    1
    23
    456
    etc
    you need a newline after 1, 3, 6 etc

    Find the formula for computing when you need to add the newline.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] While loop question
    By maple1100 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 26th, 2012, 12:42 AM
  2. While Loop quick question
    By loui345 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2012, 11:46 PM
  3. Loop Question - Very new beginner
    By Callcollect in forum Loops & Control Statements
    Replies: 12
    Last Post: January 18th, 2012, 04:01 AM
  4. [SOLVED] VERY basic question - if loop
    By kobi1988 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 12th, 2011, 06:34 PM
  5. do while loop with nested while question
    By johneusmc in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 6th, 2010, 04:45 PM