aspose file tools*
The moose likes Beginning Java and the fly likes Printing Diamon in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing Diamon in java" Watch "Printing Diamon in java" New topic
Author

Printing Diamon in java

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
I am trying to print diamond in java, but struggling a lot to get it print.

code:

public class diamond
{

public static void main ( String args[] )
{

for( int i=1;i<=4 ; i++)
{

for(int k=1; k<=i; k++)
System.out.print("*");

System.out.println();
}
//System.out.println();



for( int i=4;i>=1 ; i--)
{
//for(int j =i-1; j>=0 ;j--)
for(int k=i-1; k>=1; k--)
System.out.print("*");
System.out.println();

}




}

}
output:
**
***
****
***
**
*

I would only print the right, but unable to print left side, in order to complete diamond.
Please help me out...


Thanks<br />Dinesh
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

hint...

before you print the "*" characters, you need to print some number of spaces to move things over...


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

Joined: Aug 06, 2007
Posts: 486
But i am not geeting where should i print spaces, can you please help me out.

public static void main ( String args[] )
{

for( int i=1;i<=4 ; i++)
{

for(int k=1; k<=i; k++)
System.out.println();
System.out.print("*");
System.out.println();
}
//System.out.println();



for( int i=4;i>=1 ; i--)
{
//for(int j =i-1; j>=0 ;j--)
for(int k=i-1; k>=1; k--)
System.out.print("*");
System.out.println();

}

output
*


*



*




*
***
**
*
[ May 01, 2008: Message edited by: Dinesh Tahiliani ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39851
    
  28
Believe it or not, your 2nd attempt is a considerable improvement on the first. What your problem is, however, is what you are printing. Check what System.out.println() does when there is no argument, and think how you can alter it to get it to print out only a space.

Hint: look here, then look for PrintStream which is what System.out is, then look for its methods.
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Can you help me out please, i am passing the println without any argument only.
Please help me out guys
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39851
    
  28
Have a look at what prints the * and think how you could print a space.
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
till now I could do this only.

And it took 2 hours because I did all these things 6 years back.

And I am working on it




[ May 01, 2008: Message edited by: sachin verma ]
[ May 02, 2008: Message edited by: fred rosenberger ]

SCJP || SCWCD

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

sachin, it's nice that you can do it, but at the ranch we try to let ranchers solve their problem by giving them hints, not by pasting working code That's why you can see other ranchers giving only hints toward the solution.


[My Blog]
All roads lead to JavaRanch
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
I am facing problem in printing the space, so i need the correct direction to get it print, please help me out.. I have listen this forum helps people who are not masters and couldn't code properly.

So ranchers please please help me out.
[ May 02, 2008: Message edited by: Dinesh Tahiliani ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

we ARE trying to help you. We will NOT, however, do the work FOR you.

let's back up. forget about printing a diamond. Do you know how to just print a space? and then how to print several spaces? for example, how would you print: I'm only using the 'x' character so you can see the spaces. you should print an x, then the spaces, then another x

Note: you already know how to print something like this with your '*' characters...
[ May 05, 2008: Message edited by: fred rosenberger ]
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
I don't know how to print spaces in between, that is why i have posted here so that someone can help me out..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42627
    
  65
I don't know how to print spaces in between

If this:

prints out an asterisk character, how might that statement look if you wanted to print out a space character?


Ping & DNS - my free Android networking tools app
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14349
    
  22

Note that the method println() prints whatever you pass to it, and then it goes to the next line. So if you do this:

System.out.println(" ");
System.out.println("*");


you'll see a blank line, and a * at the beginning of the next line. If you don't want to move to the next line automatically, use print() instead of println().


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vahit Kaplanoglu
Greenhorn

Joined: May 05, 2008
Posts: 2
This makes it in a parametric way...
[ May 05, 2008: Message edited by: Ulf Dittmer ]
Vahit Kaplanoglu
Greenhorn

Joined: May 05, 2008
Posts: 2
Use the code that i have posted by considering only the odd numbers for its dimension...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Printing Diamon in java