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

Thread: Building a triangle using strings and loops...program trouble.

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Building a triangle using strings and loops...program trouble.

    Hi all, I have a project that I've been trying to do where you need to create a triangle using String objects as well as loops.

    The end result has to look like this: (the triangle needs to be complete at the bottom line but I can't get it to turn out right on this message board)

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

    We are given the example using this code:

    public class Triangle {
     
        public static void main(String[] args) {
     
            int length = 16;
     
            String border = new String(""); //declaring string object named border
     
            for(int n = 1; n <= length; n++)
            {
                border+='*';
            }
     
           //***************************************
     
            String spaces = new String("");
     
            for(int n = 1; n <= border.length()-2; n++)
            {
                spaces+=' ';
            }
     
            //****************************************
     
            int slength = 2;
     
            String sides = new String("");
     
            for(int n = 1; n <= slength; n++)
            {
                sides+='*' + spaces + "*\n";
            }
     
     
        String box = border + '\n' + sides + border + '\n';
     
        System.out.println(box);
     
        }
    }


    I have no clue how get each row to descend by one space. It's really confusing me logically especially since for loops are involved.

    This is what I have so far... even though it's not much.
    public class Triangle2 {
     
     
        public static void main(String[] args) {
     
     
           int length = 1;
     
           String tip = new String("");
     
           for(int n=1; n<=1; n++)
           {
               tip+='*';
           }
           //******************************************
     
           System.out.println(tip);
     
     
           String spaces = new String("");
     
           for(int n=1; n<=1; n++)
           {
     
           }
     
     
        }
    }

    Any help would be appreciated!


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Building a triangle using strings and loops...program trouble.

    You are most likely going to want to use nested for loops rather than separate loops.

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Building a triangle using strings and loops...program trouble.

    I understand that, but I'm confused about how to go about it. What I don't understand is that if I create a string object called spaces, how do I get it to string.length() the previous line as it builds and then add 1 space to that? I believe my logic is correct but unsure of how to set it up.

    I'm also working on another project where you have to build a rocket ship, which I have the sides but the top and bottom need to be triangles which I cannot figure out the spacing.

    This is what I have so far... (it needs to look like the top-ish part, the code below I wrote only does the sides.)

     
        /\
      /   \
    /      \
    +------+
    |       |
    |       |
    +------+
    |       |
    |       |
    +------+
    |       |
    |       |
    +------+
       /   \
     /      \
    /        \
     
     
    public class RocketShip {
     
     
        public static void main(String[] args) {
     
     
            String topbox = new String("");
     
            for(int n=1; n<=8; n++)
            {
                if(n==1 || n==8)
                {
                    topbox+="+";
                }
                else
                {
                    topbox+='-';
                }
            }
     
     
            String spaces = new String("");
            for(int n = 1; n<=topbox.length()-2; n++)
                spaces += ' ';
     
     
     
            String sides = new String("");
            for(int n = 1; n<=2; n++)
            {
             sides += '|' + spaces + "|\n";
     
            }
            System.out.println(topbox + '\n' + sides + topbox + '\n' 
                    + sides + topbox + '\n' + sides + topbox);
     
        }
    }
    Last edited by orbin; June 13th, 2012 at 03:49 PM.

Similar Threads

  1. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  2. Strings, if statements, while loops
    By ikebaseball28 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 26th, 2011, 04:45 PM
  3. [SOLVED] Nested Loops & Processing Strings
    By Usoda in forum What's Wrong With My Code?
    Replies: 20
    Last Post: October 20th, 2011, 07:15 PM
  4. Having trouble figuring out this method. Involves while-loops!
    By ayelleeeecks in forum Loops & Control Statements
    Replies: 4
    Last Post: October 19th, 2011, 08:10 PM
  5. Having trouble with strings
    By Reaperkid77 in forum Java SE APIs
    Replies: 3
    Last Post: October 20th, 2009, 06:30 PM