Results 1 to 17 of 17
  1. #1
    ace_hl is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Question basic Java looping

    What do I change here:

    public class etc{
    public static void main (String[]args){
    int x=1;
    while(x<=10){
    int y=1;
    while (y<=10){
    System.out.print(y +" ");y++;
    }
    System.out.println(x +" ");x++;
    }
    }
    }

    output:
    Java Code:
    1 2 3 4 5 6 7 8 9 10 1
    1 2 3 4 5 6 7 8 9 10 2
    1 2 3 4 5 6 7 8 9 10 3
    1 2 3 4 5 6 7 8 9 10 4
    1 2 3 4 5 6 7 8 9 10 5
    1 2 3 4 5 6 7 8 9 10 6
    1 2 3 4 5 6 7 8 9 10 7
    1 2 3 4 5 6 7 8 9 10 8
    1 2 3 4 5 6 7 8 9 10 9
    1 2 3 4 5 6 7 8 9 10 10
    to this one:

    Java Code:
    1  2  3  4  5
    6  7  8  9  10
    11 12 13 14 15
    16 17 18 19 20
    21 22 23 24 25
    -Please help.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    A single loop, rather than a double loop and an if statement using modulo (%) to determine when to print the line break.

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

    Default

    Several ways of doing it.
    Why do you have your loops going to 10 when you want 1 -5 in the first line?

  4. #4
    ace_hl is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    How to do it, please.

    -I'm confused right now, sorry.

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

    Default

    You want someone to do it for you?
    Where's the fun in that?

  6. #6
    ace_hl is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    No, just a little clue about it.

    -Wait, a single loop?
    -Our prof didn't explain it so well.

    Quote Originally Posted by masijade View Post
    A single loop, rather than a double loop and an if statement using modulo (%) to determine when to print the line break.
    -The modulo?
    -I thought its only use is to get the remainder?

    public class lines{
    public static void main (String[]args){
    int x=0;
    while(x<=??){ <------ Is 25 okay here?
    if(???){ <------- How do I use the modulo properly?
    System.out.print(x +" ");x++;
    }
    }
    }
    }
    Last edited by ace_hl; 09-02-2009 at 10:31 AM.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    you want to do the following

    Java Code:
    iteration 0 -- print num
    iteration 1 -- print num
    iteration 2 -- print num
    iteration 3 -- print num
    iteration 4 -- print num
    print line break
    iteration 5 -- print num
    iteration 6 -- print num
    iteration 7 -- print num
    iteration 8 -- print num
    iteration 9 -- print num
    print line break
    etc
    now think about how that might be done. There is a way with two loops (and math), but also a way with a single loop and an if statement.

  8. #8
    ace_hl is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    My problem is breaking the line so it prints at the next line, isn't it?

    -I'm scanning some tutorials right now.

  9. #9
    JavaVideos is offline Member
    Join Date
    Sep 2009
    Posts
    13
    Rep Power
    0

    Default

    If you want to use 2 loops then think about what you want to do:

    You want to print 5 lines of numbers.
    Within each of those lines you want to print 5 numbers.

    Can you see an inner and outer loop from there?

    Also think about the fact that the number you print out does not need to be one of the looping variables it could be a third variable if you wish.

  10. #10
    bezudar is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    code could be for your output::

    for(int i=0;i<25;i++){
    System.out.println(i);
    if(i%5==0){
    System.out.println("\n");
    }else{
    System.out.println("\t");
    }
    }

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

    Default

    Quote Originally Posted by bezudar View Post
    code could be for your output::

    for(int i=0;i<25;i++){
    System.out.println(i);
    if(i%5==0){
    System.out.println("\n");
    }else{
    System.out.println("\t");
    }
    }
    Please don't try to spoon feed people.
    Your code is wrong anyway.
    Better test your code before posting and use code tags if you have to post code.

  12. #12
    bezudar is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Please don't try to spoon feed people.
    Your code is wrong anyway.
    Better test your code before posting and use code tags if you have to post code.
    sorry abt that and thx for the input

    Java Code:
                            for(int i=1;i<=25;i++){
    			System.out.print(i);
    			if(i%5==0){
    			System.out.print("\n");
    			}else{
    			System.out.print("\t");
    			}
    			}

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by bezudar View Post
    sorry abt that and thx for the input
    So then why did you go ahead and spoonfeed again?

    The people do not learn anything when they are spoonfed with finished code and can brainlessly cut-n-paste it.

  14. #14
    ace_hl is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    What are these?

    - \n
    - \t

    Are there any other like this ones? or what are they called?
    -Oh sorry, they're called characterLiterals.
    Last edited by ace_hl; 09-02-2009 at 11:47 AM. Reason: EDIT: Oh sorry, they're called characterLiterals

  15. #15
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    they're called Escape Characters, they are used for formatting. \t is a tab, \n and \r are new lines, etc...

  16. #16
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    Quote Originally Posted by quad64bit View Post
    ... \n and \r are new lines, etc...
    Although you should prefer the system-specific newline characters provided by System properties, e.g:
    Java Code:
    System.getProperty("line.separator");
    However, for this example, it's easier just to use System.out.println() for a new line.

  17. #17
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    Modular arithmetic, also simplistically called clock arithmetic. To convert from 24 hour clock to 12 hour use 24hrtime % 12. You throw away the quotient and keep the remainder. Your 'clock' is 5 (or whatever line length). New line whenever index % n = 0. Used to divide the count into segments, with the % value being an index into that particular segment. I'm sure you can find more expansive explanations elsewhere.

    Also note that the posted code used only one loop and adapts to any data set length. double loop of 5 loops each would only handle 25 items.

Similar Threads

  1. Basic Java help, AIM?
    By jkswebsite in forum New To Java
    Replies: 4
    Last Post: 07-11-2012, 06:17 PM
  2. basic java
    By vijay24805 in forum New To Java
    Replies: 25
    Last Post: 04-14-2009, 02:46 AM
  3. Problem With Looping [JAVA]
    By jude113 in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 01:00 PM
  4. basic java help
    By adred in forum New To Java
    Replies: 0
    Last Post: 03-08-2008, 12:36 PM
  5. Java Looping and decision
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 04:29 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
  •