• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Printing Diamon in java

 
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...
 
lowercase baba
Posts: 12751
51
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 ]
 
Marshal
Posts: 64666
225
  • 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
Marshal
Posts: 64666
225
  • 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.
 
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 ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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
Posts: 12751
51
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..
 
Rancher
Posts: 43009
76
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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().
 
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...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!