Results 1 to 3 of 3
  1. #1
    Sapana's Avatar
    Sapana is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Prints Asterik in even columns and Minus in odd columns

    The Result Should be like this
    -*-*-
    -*-*-
    -*-*-





    class Printdirect
    {
    public static void main(String[] args)
    {

    String r = "";
    String u = "";
    for (int i =1; i<=3 ; i++ )

    {
    for (int j =1; j<=5 ; j++ )
    {
    if (j%2 ==0)
    {
    r = r + "*";
    r = r + "\n";
    }
    else
    {
    r = r + "-";
    r = r + "\n";
    }
    }
    System.out.println( r);
    }

    }
    }

  2. #2
    andreiutz10 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Prints Asterik in even columns and Minus in odd columns

    [ spoonfeeding deleted - Jos ]
    Last edited by JosAH; 11-30-2011 at 10:33 AM.

  3. #3
    Join Date
    Nov 2011
    Location
    Schijndel, Netherlands
    Posts
    12
    Rep Power
    0

    Default Re: Prints Asterik in even columns and Minus in odd columns

    If you put "\n" in a String, it will make a new line.
    system.out.println(); as the method says, it prints a line.

    There are 2 solutions:
    You can either get rid of the "\n".

    Or you can move the println outside the for-loop. And put the [ r = r + "\n"; ] inside the first for-loop (and delete it out of the second loop)

    That should do the trick

Similar Threads

  1. More columns in gridbox?!
    By upanotch in forum AWT / Swing
    Replies: 4
    Last Post: 10-24-2011, 11:59 PM
  2. 2D Array - Sum of columns
    By dutchgold92 in forum New To Java
    Replies: 2
    Last Post: 02-28-2011, 08:18 PM
  3. raw text to columns...
    By ashishgaj in forum New To Java
    Replies: 1
    Last Post: 02-02-2011, 07:29 PM
  4. Jlist Columns
    By DenRed in forum New To Java
    Replies: 2
    Last Post: 10-02-2009, 10:33 AM
  5. Using Columns With JTextField
    By The Evil Genius in forum AWT / Swing
    Replies: 1
    Last Post: 03-17-2008, 02:01 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
  •