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: HELP WITH FOR LOOPS! trying to concatenate without using string builder

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

    Default HELP WITH FOR LOOPS! trying to concatenate without using string builder

    I am trying to make this outcome: basically adding an asterisk more each line.

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

    but i dont want to use this code:
    *\n**\n***\n****\n*****\n etc.

    so far my code looks like this:
    message = "";
    String stars = "*";
    for (int i=1; i<8; i++)
    {
    message = message + stars + "\n";

    }
    JOptionPane.showMessageDialog(null, message);


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: HELP WITH FOR LOOPS! trying to concatenate without using string builder

    Quote Originally Posted by janeeatsdonuts View Post
    I am trying to make this outcome: basically adding an asterisk more each line.

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

    but i dont want to use this code:
    *\n**\n***\n****\n*****\n etc.

    so far my code looks like this:
    message = "";
    String stars = "*";
    for (int i=1; i<8; i++)
    {
    message = message + stars + "\n";

    }
    JOptionPane.showMessageDialog(null, message);
    Great.

    Now do you have an actual question that we can answer?

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

    Default Re: HELP WITH FOR LOOPS! trying to concatenate without using string builder

    uhm.
    the output for my code is:
    *
    *
    *
    *
    *
    *
    *
    instead of

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

    i want to make
    *
    *
    *
    *
    *
    *
    *
    into
    *
    **
    ***
    ****
    *****
    ******
    *******
    how do i make it into:
    *
    **
    ***
    ****
    *****
    ******
    *******

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: HELP WITH FOR LOOPS! trying to concatenate without using string builder

    Figure out the logic first. I would do something like:

    On row 0 I want 1 star
    On row 1 I want 2 stars
    on row 2 I want 3 stars
     
    ... 
     
    on row x I want (x + 1) stars
     
    ... 
     
    stop at row y.

    Then translate that into pseudo code

    begin loop that will loop y times.
      print (loop index + 1) stars
      print new line
    end loop

    That line "print loop index + 1 stars can be further broken down into its own loop.

    begin loop that will loop y times.
      // print (loop index + 1) stars
      create an inner loop that loops from 0 to the current (outer loop's index + 1)
        print a "*"
      end inner loop
     
      print new line
    end loop

    Now you try to translate the pseudo-code into Java code.

Similar Threads

  1. How to use Builder Setters
    By copeg in forum Java Programming Tutorials
    Replies: 3
    Last Post: January 19th, 2012, 06:40 PM
  2. Am I doing it rite? Using netbeans GUI builder
    By erm3r in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2011, 11:48 AM
  3. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 09:16 AM
  4. Process Builder Issue
    By javameanslife in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 20th, 2010, 09:13 PM

Tags for this Thread