how to print

how to print the following in java?***************please help me...
[116 byte] By [kannabiran.krisha] at [2007-11-27 7:09:03]
# 1
Print as in System.out.println(...)?
thomas.behra at 2007-7-12 19:00:30 > top of Java-index,Java Essentials,New To Java...
# 2
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
prometheuzza at 2007-7-12 19:00:30 > top of Java-index,Java Essentials,New To Java...
# 3

The solution is simple... as below

class printStars {

public static void main(String args[]) {

String strString = "";

for(int i=0; i< 5;i++)

{

strString = "";

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

{

strString+="*";

}

System.out.println(strString);

}

}

}

java_techya at 2007-7-12 19:00:30 > top of Java-index,Java Essentials,New To Java...
# 4
seee the below exampleclass StarsExample {public static void main(String arg[]) {for(int i=1;i<5;i++) {for(int j=0;j<i;j++) {System.out.print("*");}System.out.print("\n");}}}>
Saikumara at 2007-7-12 19:00:30 > top of Java-index,Java Essentials,New To Java...
# 5

> The solution is simple... as below

And what did the OP learn from this?

> class printStars {

> public static void main(String args[]) {

>String strString = "";

> for(int i=0; i< 5;i++)

>{

>strString = "";

>for(int j=0;j<=i;j++)

>{

>strString+="*";

> }

>System.out.println(strString);

> }

> }

It's a bad example: you use magic numbers and you don't use Java's code conventions.

prometheuzza at 2007-7-12 19:00:30 > top of Java-index,Java Essentials,New To Java...
# 6

Hi prometheuzz,

Please let me know if the below code looks better. If you see any standards not being followed, could you please let me know?

import java.lang.*;

public class PrintStars {

public static void main(String args[]) {

PrintMyStar objPrntMyStar = new PrintMyStar(5,"*");

System.out.println(objPrntMyStar.prepareOutput());

}

}

class PrintMyStar {

private StringBuffer strStarBuff;

private int iRows;

private String strToPrnt;

static final int DEF_ROWS = 5; //Default number of rows

static final String STAR = "*";

static final String NEW_LINE = "\n";

/*

Default constructor

*/

PrintMyStar()

{

strStarBuff = new StringBuffer();

iRows = DEF_ROWS;

strToPrnt = STAR;

}

/*

Constructor only to initialize number of rows

*/

PrintMyStar(int iNoOfRows)

{

strStarBuff = new StringBuffer();

iRows = iNoOfRows;

strToPrnt = STAR;

}

/*

Constructor to initialize number of rows & string to print

*/

PrintMyStar(int iNoOfRows, String strToPrnt)

{

strStarBuff = new StringBuffer();

iRows = iNoOfRows;

this.strToPrnt = strToPrnt;

}

public String prepareOutput()

{

for(int i=0; i<iRows;i++)

{

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

{

strStarBuff = strStarBuff.append(strToPrnt);

}

strStarBuff.append(NEW_LINE);

}

return strStarBuff.toString();

}

}

>

java_techya at 2007-7-12 19:00:30 > top of Java-index,Java Essentials,New To Java...
# 7
> If you see any standards not being followed, could you please let me know?Well, since you asked so nicely:... main(String args[]) { ...is a C-style array declaration. The Java-way would be... main(String[] args) { ...
thomas.behra at 2007-7-12 19:00:30 > top of Java-index,Java Essentials,New To Java...