Results 1 to 4 of 4
  1. #1
    pantaloc is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Noob Question - printing loop

    Hi, I'm new to the forum and a completely new to java so please be too critical ^^!

    What I'm trying to do is to print 3 blank lines, then 3 lines of 13 dots under it, followed by another 2 blank lines and finally a line of 13 underscores.

    Since I'm a complete noob, this messy piece of code is what I've got so far. Would anyone be kind enough to help me simplify it please? Any help will be appreciated!

    Java Code:
            for (int y = 1; y <= 3; y++)
            {
                for (int x = 0; x < 13; x++)
                {
                    System.out.print(" ");
                }
                System.out.println();
            }
            for (int y = 4; y <= 6; y++)
            {
                for (int x = 0; x < 13; x++)
                {
                    System.out.print(".");
                }
                System.out.println();
            }
            for (int y = 7; y <= 8; y++)
            {
                for (int x = 0; x < 13; x++)
                {
                    System.out.print(" ");
                }
                System.out.println();
            }
            for (int x = 0; x < 13; x++)
            {
                System.out.print("_");
                System.out.println();
            }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Noob Question - printing loop

    1. Does a 'blank line' mean 13 space characters? I wouldn't interpret it that way/
    2. Have you learned to use methods? I would make a method for printing 13 times (on the same line) the character passed to it as a parameter and call that method from inside a loop. That would move the inner loops to the new method and make the code cleaner.
    3. Have you learned to use arrays? You could put the character for each line in an array and iterate over the array calling the method described above. That would reduce the main code to a single loop.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    pantaloc is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Noob Question - printing loop

    Hello sir and thank you very much for your quick reply.

    I will try to create the method you hinted me to. About the arrays, I have not learned much about arrays, would you mind to provide me with an example of how to approach this issue please? I will be very very appreciated :D

    Or is there anyway to do it like this:

    Assume x is the number of characters to be printed, and y is the number of lines. Create a loop so that on line 1-3 print blanks, line 4-6 print dots, line 7-8 print blanks and line 9 print underscores.
    Last edited by pantaloc; 04-28-2012 at 05:19 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Noob Question - printing loop

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. noob question!
    By swp in forum New To Java
    Replies: 5
    Last Post: 09-29-2011, 12:14 AM
  2. noob question. plz help tho!
    By swp in forum New To Java
    Replies: 2
    Last Post: 09-28-2011, 07:16 AM
  3. Probably Really Noob Question
    By bpx95 in forum New To Java
    Replies: 2
    Last Post: 05-16-2011, 02:44 AM
  4. Noob button question.
    By dudejonne in forum Java Applets
    Replies: 4
    Last Post: 10-20-2009, 11:51 PM
  5. Question - I'm a noob!
    By Insaeno in forum New To Java
    Replies: 5
    Last Post: 08-04-2008, 04:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •