• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java for loop help

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to beginners forum.

Henry
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic