Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

A lil help!

Options
  • 17-05-2005 7:27pm
    #1
    Registered Users Posts: 2,601 ✭✭✭


    I'm just kinda stuck at the mo. I know this will come up in the exam tomorrow but i cant remember how to do it.

    Write a program to Print out the following using Loops in Java. I know its a for loop. Heres the pattern.

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

    Heres the code i already have written:

    public class Stars
    {
    public static void main(String args[])
    {
    int i, j;
    for(i=0; i<10; i++)
    {
    for(j=0; j<10; j++)
    {
    System.out.print("*");
    }

    System.out.println();
    }
    }
    }

    but it keeps printing out like this:

    **********
    **********
    **********
    **********
    **********
    **********
    **********
    **********
    **********
    ********** :(


«1

Comments

  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    You want to inner loop to print i + 1 stars on each line basically


  • Registered Users Posts: 3,958 ✭✭✭Chad ghostal


    Havent tried it, but it looks like j should be incremented in the outer 'i' loop and not in its own loop..if you get me..


  • Registered Users Posts: 16,402 ✭✭✭✭Trojan


    It sure does. Think about why it's printing 10 on the 1st line.

    You want to print 1 on the 1st, 2 on the 2nd, 3 on the 3rd, right? What variables do you need to use while printing?

    btw if you use [ code ] tags around segments it'll show properly.

    --

    Aside: If anyone gives a straight solution on this thread without helping the (OP) student learn something their prize is a ban from Programming :)


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    Trojan wrote:
    Aside: If anyone gives a straight solution on this thread without helping the (OP) student learn something their prize is a ban from Programming :)
    I'd usual agree with the "teach a man to fish..." arguement, but it looks like (s)he is doing some last minute exam cramming. Have a heart :)


  • Registered Users Posts: 16,402 ✭✭✭✭Trojan


    Bah, should have some clue. It's simple logic, though for a 1st year with summer exam tomorrow it might be tough.

    I retract the prize as offered.


  • Advertisement
  • Registered Users Posts: 3,958 ✭✭✭Chad ghostal


    meh... if ..kittykat still needs it...
    which is unlikely..

    (only took me 3 goes :o)
    public class Stars {
    
      public static void main(String args[])
      {
      int i, j;
      for(i=0; i<10; i++)
      {
    
      for(j=0; j<i+1;j++ )
      {
      System.out.print("*");
      }
    
      System.out.println();
      }
      }
    }
    
    


  • Closed Accounts Posts: 92 ✭✭tempest


    Trojan wrote:
    Bah, should have some clue. It's simple logic, though for a 1st year with summer exam tomorrow it might be tough.

    I retract the prize as offered.

    I think you were right regardless of cramming.

    The exam could well consist of a question based on the thought processes used in the first, (although experience implies otherwise).

    For example, somebody here writes the code and then the exam says that the output should be:

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

    If the answer is given then the OP will not have gained the necessary approach to solve this themselves.

    tbph, and IMHO, the most information that should be given is to comment each line of code.
    public class Stars
    {
      public static void main(String args[])
      {
        int i, j; // Counter for the inner and outer loops.. maybe these can be named better? line, starCounter
    
        // For each line that I have to print stars on
        for(i=0; i<10; i++)
        {
          // For 10 stars? 
          for(j=0; j<10; j++)
          {
            // Print a single star
            System.out.print("*");
          }
    
          // Move to the next line
          System.out.println();
        }
      }
    }
    


  • Registered Users Posts: 2,601 ✭✭✭MidnightQueen


    for(j=0; j<i+1;j++ )

    Thanks for solving the problem. I knew i was missing something. Thanks for that! It should come up in the exam i hope.


  • Registered Users Posts: 16,402 ✭✭✭✭Trojan


    KittyKat wrote:
    Thanks for solving the problem. I knew i was missing something. Thanks for that! It should come up in the exam i hope.
    I'm not convinced this was a worthwhile exercise...


  • Registered Users Posts: 3,010 ✭✭✭BizzyC


    If you do it this way, it'll work in both directions.
    
      int x= 1; //set to 10 if you need to print reverse pyramid 
     for(int i =0; i<10; i++)
     {
       for(int a=0;a<x;a++)
       {
          System.out.print("*");
       }
       System.out.println("");
       x++; // set to x-- if pyramid is in reverse order.
     }
    

    Should print
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********

    change x values as stated and it should give the reverse.


  • Advertisement
  • Registered Users Posts: 2,601 ✭✭✭MidnightQueen


    Sorry to say it didnt come up in the exam. :( Pitty cos i was hoping it would come up. Nested Loops came up instead. Thanks for all the help! ;) If theyre was still rep going around i would still give some to all. :)


  • Registered Users Posts: 3,010 ✭✭✭BizzyC


    Is a for loop inside another loop not a nested loop???


  • Registered Users Posts: 2,601 ✭✭✭MidnightQueen


    Ok ya you're rite.* For loops did come up, but i was hoping that they would have the Question to "print a Triangle using loops" would come up and it didnt. :(







    *smartass! :D


  • Registered Users Posts: 3,010 ✭✭✭BizzyC


    Was I being a smartass!?!:p


  • Registered Users Posts: 2,601 ✭✭✭MidnightQueen


    U are now!*






    *Goes to fetch the whip. :p










    Nah i'm only messing! Thanks for trying to help out. :)


  • Registered Users Posts: 3,010 ✭✭✭BizzyC


    Hope you passed.
    Good luck.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    KittyKat wrote:
    I'm just kinda stuck at the mo. I know this will come up in the exam tomorrow but i cant remember how to do it.

    Ah, I wish my exams were going to be like that...


  • Closed Accounts Posts: 839 ✭✭✭zap


    if you were doing the exam i did, it was very easy and a q like that did come up except it was numbers instead of stars.


  • Registered Users Posts: 2,601 ✭✭✭MidnightQueen


    BizzyC wrote:
    Hope you passed.
    Good luck.

    Thanks! :)
    zap wrote:
    if you were doing the exam i did, it was very easy and a q like that did come up except it was numbers instead of stars.

    Ya i hate the ones with numbers.


  • Closed Accounts Posts: 4 uber


    For what it's worth, here's the one loop version.
    Can it be done any shorter? (style notwithstanding)
    public class Stars
    {
    
        public static void main(String[] args)
        {
            //the accumulator
            String ssOutput = new String();
            // the character for the pattern
            String ssCHAR_TO_OUTPUT = new String("*");
            // the count of lines
            int iNumLines = 10;
            //loop around
            for ( int i = 0 ; i < iNumLines; i++)
            {
                //add the character to the accumulator
                ssOutput += ssCHAR_TO_OUTPUT;
                //print out the accumulator with the newline
                System.out.println(ssOutput);
            }
        }
    }
    


  • Advertisement
  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    uber wrote:
    Can it be done any shorter?

    Don't think so, but you could probably improve performance by generating it entirely in memory (line-feeds included) and writing once.

    Or, you could do it all ultra-dirty stylee, which is effectively still just your code.
     public static void main(String[] args)
    	{
    		for ( String ssOutput= new String() ; ssOutput.Length < 10 ; system.out.println(ssOutput+="*") );
    	}
    

    Thats just nasty though :)


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    uber wrote:
    For what it's worth, here's the one loop version.
    Can it be done any shorter? (style notwithstanding)

    You should be using StringBuffer.


  • Registered Users Posts: 2,426 ✭✭✭ressem


    Now you've gone and done it,

    I might as well be the first to suggest
    "*\n**\n***\n****\n...


    For short,
    How about set ssCHAR_TO_OUTPUT to "**********"
    and use a substring in the loop

    System.out.println(ssCHAR_TO_OUTPUT.substring(i))

    No superflous manual new, or string/stringbuffer argument.
    Get promoted before you have to maintain it tho.


  • Registered Users Posts: 27,088 ✭✭✭✭GreeBo


    ressem wrote:
    No .....string/stringbuffer argument.
    substring will create a new String object each time....


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Where on earth did you dig this up from?


  • Registered Users Posts: 27,088 ✭✭✭✭GreeBo


    sometimes being right is more important than being timely... :D


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    GreeBo wrote:
    sometimes being right is more important than being timely... :D

    Is it relevant, though? He didn't say it didn't create an object.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    rsynnott wrote:
    Is it relevant, though? He didn't say it didn't create an object.

    Hes right. Every time you make a reference to create/update a string it always creates a new object which is wasteful. StringBuffer doesn't.

    Of course both will work though and for such a tiny program its no big deal but you might as well learn correctly from the start.

    I like the "*\n**\n***\n..." approach though. :)


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Hobbes wrote:
    Hes right. Every time you make a reference to create/update a string it always creates a new object which is wasteful. StringBuffer doesn't.

    Of course both will work though and for such a tiny program its no big deal but you might as well learn correctly from the start.

    I like the "*\n**\n***\n..." approach though. :)

    He's right, all right, at least theoretically. I believe, though, that at least Sun's compiler has optimised inappropriate use of String to StringBuffer for some time now.


  • Advertisement
  • Registered Users Posts: 27,088 ✭✭✭✭GreeBo


    rsynnott wrote:
    I believe, though, that at least Sun's compiler has optimised inappropriate use of String to StringBuffer for some time now.
    Is it smart enough to catch looped substring calls?
    If it is thats impressive.


Advertisement