Results 1 to 11 of 11
  1. #1
    ukemasta is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default add a space after 10 lines?

    Hi I have a for loop, but I need to add a space after the first 10 positions. Anyone know how this can be done?
    Java Code:
    public void one()
        {
            // put your code here
            System.out.println("Value   Character");
            for(int i = 32; i < 127; i++){
                System.out.println( i );
            }
        }
    Thanks for your help

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    if statement should help here. Try it.

  3. #3
    ukemasta is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    would I put it after the println or before?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Try it all and see.
    You don't lose a dollar every time you try something on your compiler, do you?

  6. #6
    ukemasta is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    i've been trying. I new to java so dont know much, but it would make it easier if I knew where to put it because every little mistake makes it not compile.

    I cant seem to get the if statement right. This is what I have so far. I dont know how to tell it to print that space after the 10th line. This is what my if statement looks like so far.

    Can you please guide me in the right direction?

    thank you

    Java Code:
    public void one()
        {
            // put your code here
            System.out.println("Value   Character");
            for(int i = 32; i < 127; i++){
                System.out.println( i );
                if( i < 127 &&){
                    System.out.println(" ");
                }
            }

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Of course it doesn't compile like that. What is the && supposed to be doing in there?
    Also you said print space after 10 positions. So the if should be trying to say if this is now the 10th position then print a space.
    You have an index i which is incrementing. If you started at 32 then you whould work out what i will be after ten "positions". That's what you use to control your if.

  8. #8
    Join Date
    Dec 2009
    Location
    India
    Posts
    6
    Rep Power
    0

    Default

    u can also implement a counter say c which will keep the track of ur line no.
    lyk
    System.out.println("Value Character");
    int c=0;
    for(int i = 32; i < 127; i++)
    {
    System.out.println( i );
    c++;
    if (c%10==0)
    System.out.println();
    }

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Why introduce a new counter when there is already an index?

  10. #10
    Join Date
    Dec 2009
    Location
    India
    Posts
    6
    Rep Power
    0

    Default

    using counter...i always find it easy..i mean to say if i m using the same index i thn dont u think the logic wd be complex

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Help explain the FOR lines please
    By hydride in forum New To Java
    Replies: 2
    Last Post: 01-20-2010, 12:52 AM
  2. Replies: 0
    Last Post: 07-05-2009, 01:31 PM
  3. Anyway to fix the lines so they dun shift?
    By PeterFeng in forum New To Java
    Replies: 0
    Last Post: 01-14-2009, 11:26 AM
  4. Random Lines
    By Urgle in forum New To Java
    Replies: 29
    Last Post: 11-12-2008, 04:42 PM
  5. how to edit lines.
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 05:41 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
  •