Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# write a method called printTriangle. How to start?

Posted on 2008-10-20
Medium Priority
239 Views
(Objects first with java, A practical Introduction using Blue J, Third Ed, David J. Barnes & Michael Kolling)

Hi

I have to write a method called printTriangle.

example:  printTriangle(4);

output should look like this:

*
**
***
****

and I don't know how to start. Can you please give me an advice?
0
Question by:bt1942
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2
• +2

LVL 20

Expert Comment

ID: 22764318
0

Author Comment

ID: 22764354
chaituu// that seems very complicated, I just need to write a simple method soething like

``````public void printTriangle()
{
int n = 4;
String s = "*";
for(int row = 1; row<= n; row++)
{
System.out.println(s);

}
}
}
``````
0

LVL 6

Accepted Solution

javaexperto earned 800 total points
ID: 22764396
the first thing you need is one loop to print "*" and other loop to print a new line. Your first loop will get up to your parameter and the second will enter the first time one,  the second two, etc. The loops must be nested, here the method:
``````public void printTriangle(int stars) {
for(int i = 0; i < stars; i++) {
for(int j = 0; j <= i; j++)  {
System.out.print("*");
}
System.out.println("");
}
}
``````
0

LVL 3

Assisted Solution

siranm earned 400 total points
ID: 22764405
here you go....

``````public void printTriangle()
{
int n = 4;
String s = "*";
for(int row = 1; row<= n; row++)
{

for(int stars=1; stars<=row;start++){
System.out.println(s); //1st row->1star, 2nd row->2stars,...
}
}
}
``````
0

LVL 3

Expert Comment

ID: 22764410
yep, javaexperto, i missed that detail on the new line ; )
0

LVL 3

Assisted Solution

glenjr earned 400 total points
ID: 22764419
bt1942,

you are on the right track. right now your program prints out:

*
*
*
*

In order to adjust it so that it prints out the way you want you need to the following line below your system.out...:

s+= "*";

Also, make sure your {} match and that you have a main method in there some where.

Let me know if you have questions.

glenjr
0

LVL 3

Expert Comment

ID: 22764424
looks like i wasnt the only one working on it, i wasnt fast enough :)
0

LVL 20

Assisted Solution

chaitu chaitu earned 400 total points
ID: 22764430
public static void main(String[] args) {

int n = 4;
String s = "*";
String s1 = "";
for(int row = 1; row<= n; row++)
{

for(int stars=1; stars<=row;stars++){
s1+=s;
System.out.println(s1); .
}
}
}
0

Author Closing Comment

ID: 31508140
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different dâ€¦
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month8 days, 11 hours left to enroll