• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java for loop help

 
Amir Ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible to print out the patter below using one and only one for loop in one dialog box? Do not use nested for loops. Use only one for loop, not two or more. Do not use any other kind of loop. Do not use a switch/case statement or if conditions.

*
**
***
****
*****
******
*******

I can print it using nested loop, while loop and so forth and i'm having a challenge trying to print it using one for loop only.

Thanks,
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does this have to do with threading?

And where is your code? What problems are you having with it?
 
Amir Ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the posting on the wrong section. I've just joined this forum.

Here's what i have so far and it works fine. I want to use string concatenation for *

String stars = " ";
for(int i = 1; i <= 7; i++;)
{
stars = stars + "*\n**\n***\n****\n*****\n******\n*******";
}
JOptionPane.showMessageDialog(null, stars);

Is there a way to add to have the loop do the * rather than writing it like i did.
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to beginners forum.

Henry
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amir Ahmed wrote:Is there a way to add to have the loop do the * rather than writing it like i did.


Is there a way? Yes, I came up with 3. The ways I came up with (in addition to yours) include:

1) Using an array of Strings
2) Using the Arrays.fill(char[], char) method
3) Using a Format String

I am sure there are other ways.
 
Tony Docherty
Bartender
Posts: 2939
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what i have so far and it works fine.

Surely that produces the pattern 7 times over.
There is a way to do it with concatenation but you need to use 2 separate Strings, one to hold the growing line of asterisks and one to concatenate each line of asterisks to.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amir,

Amir Ahmed wrote:Hello Steve, thanks for your response. As i'm really new to java, i was wondering if there is any way you can write the code for your suggestions because im having a hard time doing so. Thanks again!


In the future, please UseTheForumNotEmail (or mooseages), thanks. We are NotACodeMill here at the ranch. We will try to help you but won't give you the code. I gave some hints on the first three ideas that came to mind, and Tony gave another great idea. Pick one and try to implement it. Then we can help you with specific questions you might have.
 
Amir Ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your responses guys! I really appreciate it.

Tony, i used your two strings suggestion and it was much easier than i thought.

Steve, my apologies. Probably won't happen again.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic