File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to create this 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 "how to create this in java" Watch "how to create this in java" New topic
Author

how to create this in java

james maina
Greenhorn

Joined: May 31, 2012
Posts: 7
*
**
***
****
*****
****
***
**
*
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

Hi James. Welcome to the Ranch!

It's a much more effective learning process if you manage this yourself (with help if necessary) than if someone hands out the answer. So...what have you tried so far? Where are you having problems?
james maina
Greenhorn

Joined: May 31, 2012
Posts: 7
I have just started created few programs but when i came to this one i tried to do this
[code=java]class Patterns{
public static void main(String[] args){
System.out.println("*");
System.out.println("*");
System.out.println("**");
System.out.println("***");
System.out.println("****");
System.out.println("*****");
System.out.println("****");
System.out.println("***");
System.out.println("**");
System.out.println("*");
}
}
Rose Ellis
Greenhorn

Joined: Jun 08, 2012
Posts: 16

Well you could take a manual approach and write 9 print statements. That could be your start. Or you could try making it a little bit more automatic and use a for loop with a couple of if statements to determine whether you're increasing the number of stars or reducing them and print them out. Give it a shot and let us know if you get stuck


If a cluttered desk is a sign of a cluttered mind, then what are we to think of an empty desk?
[Albert Einstein] - or just another way to justify my mess
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

And what happened? Did you get any errors?

Now I've formatted it I can see a couple of problems. One with the name of the class (you can't have * as part of a class or variable name). And have a closer look at line 2 - does that look like the start of other main methods you've seen?
BalaMurali dhar
Ranch Hand

Joined: Apr 14, 2012
Posts: 60
The Program comes likes this
public class Patterns {
public static void main(String[] args) {
System.out.println("*");
System.out.println("**");
System.out.println("***");
System.out.println("****");
System.out.println("*****");
System.out.println("****");
System.out.println("***");
System.out.println("**");
System.out.println("*");
}
}
james maina
Greenhorn

Joined: May 31, 2012
Posts: 7
that was great....but now how do i use the for loop to make it automatic?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

What have you tried?

We really don't hand out answers here. Make an effort, write some code, post it, tell us what it did, tell us what you want it to do...

Simply saying "Please do this for me" really won't get you very far around these parts.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rose Ellis
Greenhorn

Joined: Jun 08, 2012
Posts: 16

I'll give you some pseudocode

loop until it reaches the number of lines you need to print out
in the loop
concatenate the "*" to a String variable (has to be initialised outside the loop) if the loop counter is less than the longest line number,
otherwise use a substring starting from position 1 (that would remove one "*" from your string)
print the resulting string

done
james maina
Greenhorn

Joined: May 31, 2012
Posts: 7

Thanks Rose, that is idea I needed....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Rather than catenating to a String, append a * to a StringBuilder, or remove a * from it. That will give better performance.
james maina
Greenhorn

Joined: May 31, 2012
Posts: 7
class Patterns {
public static void main(String[] args){
for(int i=1; i<10; i++){
for(int j = 1;j <= i;j++){
System.out.print("*");
}
System.out.println(" ");
}
}
}


guys I managed to do this but the part that is supposed to make them reverse the other way has proved to be a bit hectic.
james maina
Greenhorn

Joined: May 31, 2012
Posts: 7
public class pattern3{
public static void main(String [] args){
for(int i=1; i<=12; i++){
if(i>1 && i<11){
for(int j=7; j<12-Math.abs(i-6); j++)
System.out.print("*");

}
System.out.println();
}
}
}
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

james maina wrote:guys I managed to do this but the part that is supposed to make them reverse the other way has proved to be a bit hectic.

Ah, so now you're changing the rules, eh?

Have another look at Campbell's suggestion, and then look at the API for java.lang.StringBuilder. I think you may find exactly what you're looking for, but you MUST look.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

This is kind of mind-boggling, isn't it? Come back to it in a day or two and try to figure out what it's doing. Or have someone else read it and figure out what it's doing. Not nice.
It's easier to solve if you break down the problem into smaller parts:
If you strive for clarity, the solution comes a lot quicker and it turns out a lot nicer.


Junilu - [How to Ask Questions] [How to Answer Questions]
Ashwin Soni
Greenhorn

Joined: Jun 22, 2012
Posts: 18

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

Ashwin Soni,

First, welcome to the Ranch. We're glad you decided to stop by.

Second, we generally frown upon providing complete solutions. We want to teach people how to program, not do their homework for them. Solutions will generally be deleted. However, since this doesn't actually do what the OP asked for, I'll go ahead and leave it. This is what I got for the output:

C:\slop>java Arrow
*
**
***
****
*****
******
*******
********
*********
******************************************************
Ashwin Soni
Greenhorn

Joined: Jun 22, 2012
Posts: 18

fred rosenberger wrote:Ashwin Soni,

First, welcome to the Ranch. We're glad you decided to stop by.

Second, we generally frown upon providing complete solutions. We want to teach people how to program, not do their homework for them. Solutions will generally be deleted. However, since this doesn't actually do what the OP asked for, I'll go ahead and leave it. This is what I got for the output:

C:\slop>java Arrow
*
**
***
****
*****
******
*******
********
*********
******************************************************


Yes, Actually I understood by reading all replies in the post that we are not doing their homework.
That is why I left following code at 99.99% .
However, In code you can understand Loop is right but braces are at wrong position therefore it happened.
james maina
Greenhorn

Joined: May 31, 2012
Posts: 7
Hi guys it was a great experience to learn how to create that code with for loop and an if statement.......I had posted the progress of my work, and still working on other methods and I will post them to show the progress..... I appreciate
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to create this in java