Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing Diamon in java

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hint...

before you print the "*" characters, you need to print some number of spaces to move things over...
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you help me out please, i am passing the println without any argument only.
Please help me out guys
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at what prints the * and think how you could print a space.
 
sachin verma
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Vahit Kaplanoglu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This makes it in a parametric way...
[ May 05, 2008: Message edited by: Ulf Dittmer ]
 
Vahit Kaplanoglu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the code that i have posted by considering only the odd numbers for its dimension...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic