File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to make this as an output ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to make this as an output ?" Watch "how to make this as an output ?" New topic
Author

how to make this as an output ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

how to print this


[Thumbnail for sas.png]


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

What have you tried?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

fred rosenberger wrote:What have you tried?


soory i didnt mentioned my code
want to get above output by modifying this code
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

dont think of it as trying to print a triangle, think of it as trying to print out a rectangle partially filled with spaces.



Now look at the number of spaces (marked by '-' ) versus the number of stars on each line. There is a pattern.

Hunter


EDIT: fixed typo


"If the facts don't fit the theory, get new facts" --Albert Einstein
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

to take Hunters comment just a little further, I would look at the number of spaces vs the number of star vs. what line number you are printing.

And, I'd start counting the lines on line 0. So, fill this out:

on line 0, i have ___ spaces and ___ stars
on line 1, i have ___ spaces and ___ stars
etc.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

Hunter McMillen wrote:dont think of it as trying to print a triangle, think of it as trying to print out a rectangle partially filled with spaces.



Now look at the number of spaces (marked by '-' ) versus the number of stars on each line. There is a pattern.

Hunter


EDIT: fixed typo

but i need to implement this only with the help of " for " loop , no other loops or control structures.
then how to do this , the above programe is typing the stars from right to left
but in this we need to put a 4 empty space in 0 line and then we have to reduce the space , how to achieve this with for loops.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

naved momin wrote:but i need to implement this only with the help of " for " loop , no other loops or control structures.
then how to do this , the above programe is typing the stars from right to left
but in this we need to put a 4 empty space in 0 line and then we have to reduce the space , how to achieve this with for loops.

You know how to print stars "*", you should also know how to print spaces " ".
You know how to increase number of stars, you should also know how decrease number of spaces.
Follow fred rosenberger advice on "fill in the blanks" before code.
Coding is simply turning algorithm into program, you have to find out the algorithm first.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

Hunter McMillen wrote:dont think of it as trying to print a triangle, think of it as trying to print out a rectangle partially filled with spaces.



Now look at the number of spaces (marked by '-' ) versus the number of stars on each line. There is a pattern.

Hunter


EDIT: fixed typo

i have made it ,. tricky , but still succeded just with the help of for loop
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

naved momin wrote:
i have made it ,. tricky , but still succeded just with the help of for loop

Try to remove all the for statements and leave only the print and println statements.
There is a way to make use of for loop to print space x times and print star y times where x + y = 4.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

Raymond Tong wrote:
naved momin wrote:
i have made it ,. tricky , but still succeded just with the help of for loop

Try to remove all the for statements and leave only the print and println statements.
There is a way to make use of for loop to print space x times and print star y times where x + y = 4.

i didnt get you actually
as you had said , i have tried , will you throw some solution to this .

it is just printing a star one below the other.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

naved momin wrote:i didnt get you actually
as you had said , i have tried , will you throw some solution to this .
it is just printing a star one below the other.

I meant the multiple for loops with label break didn't really make use of the power of for loops.
i.e. You have for loop statement in your code but they all iterate once.

Try to turn the following pseudocode into java code
step 1. iterate (x) from 1 to 4
step 2. print (4-x) spaces
step 3. print (x) stars
step 4. print "line break and/or carriage return"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Raymond Tong wrote: ... step 4. print "line break and/or carriage return"
You mean "Terminate[s] the current line by writing the line separator string".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to make this as an output ?
 
Similar Threads
Ant and Cactus - classpath issues
http-method not specified
Clock problem
thanks!!! another SCJP born~
Args in Netbeans