wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes java for loop help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java for loop help" Watch "java for loop help" New topic
Author

java for loop help

Amir Ahmed
Greenhorn

Joined: Feb 19, 2013
Posts: 17
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

What does this have to do with threading?

And where is your code? What problems are you having with it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Amir Ahmed
Greenhorn

Joined: Feb 19, 2013
Posts: 17
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
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Moving to beginners forum.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

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.


Steve
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1939
    
  28
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

Joined: Jan 28, 2003
Posts: 3947
    
  17

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

Joined: Feb 19, 2013
Posts: 17
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.
 
wood burning stoves
 
subject: java for loop help
 
Similar Threads
Assignment 2.1 - Times
how to print triangle like format
Assignment 1B
Small looping question
nested loop ?