aspose file tools*
The moose likes Beginning Java and the fly likes Understanding logic on for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Understanding logic on for loop" Watch "Understanding logic on for loop" New topic
Author

Understanding logic on for loop

saravanakumar rajendran
Greenhorn

Joined: Nov 14, 2005
Posts: 13
Hi team,
can some one give me clue for printing this
*****
s****
ss***
sss**
ssss*

where s is space.
using for loop
since i am totally mad to achieve this, atlast i posted here
[ November 09, 2006: Message edited by: saravanakumar rajendran ]
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Ok, let's look at it:

Obviously, we need a loop of some sort to acheive this. Further investigation
of the output tells me we need 5 iterations.



Does this help you? if not, where are you stuck - code wise.

/Svend Rost
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Well I would use three for loops (although I'm sure there are alternative solutions). Consider the pattern looks like:

0 spaces : 5 stars
1 space : 4 stars
2 spaces : 3 stars
...



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
saravanakumar rajendran
Greenhorn

Joined: Nov 14, 2005
Posts: 13
for (int k=1;k<=5;k++)
{
for (int l=k+1;l<=5 ;l++ )
{
System.out.print("*");
}
System.out.println();

}

for this i am getting

****
***
**
* not as i expected one.
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Originally posted by saravanakumar rajendran:
for this i am getting

****
***
**
* not as i expected one.


Im using a single loop solution.

String s = "*****"
and at the end we want
s="....*" , where . = " "

Each loop we want to replace a * with a " ". Try to look in the API for
the String class for a usefull method..
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

Does this help you?
saravanakumar rajendran
Greenhorn

Joined: Nov 14, 2005
Posts: 13
thanks a lot man.
saravanakumar rajendran
Greenhorn

Joined: Nov 14, 2005
Posts: 13
that api is technically advance, i need it for beginners level.
any way thanks a lot for your kind reply
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Originally posted by saravanakumar rajendran:
for (int k=1;k<=5;k++)
{
for (int l=k+1;l<=5 ;l++ )
{
System.out.print("*");
}
System.out.println();

}

for this i am getting

****
***
**
* not as i expected one.


Hi saravanakumar,

you didn't follow the advice Garrett gave you:
for (i = 0 -> totalNumberOfLines)
print i spaces
print (totalNumberOfLines - i) stars
print a new line character

You did this:

hint: you forgot to print the i spaces
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Originally posted by saravanakumar rajendran:
that api is technically advance, i need it for beginners level.
any way thanks a lot for your kind reply


Fair enough.. although I would suggest you to learn the API, or atleast
know how to look in it and search for usefull methods and information
about the different classes.

A more simple solution could be to operate with two strings. This solution
is based on the advice you got from Rowe.
saravanakumar rajendran
Greenhorn

Joined: Nov 14, 2005
Posts: 13
thanks guys, i got it now
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
Hi to all.
I am new to JavaRanch. I am planning to take the SCJP exam (CX-310-055) by the end of Decemeber. I usually come here to see other people's problems, questions, etc. & try to solve them on my own. This helps me check my concepts in various fields.
I read the first post and made out the solution in just 2 minutes
Here is the code for my Diamond.java in (...)\language\looping\ directory:- (Sorry! The code is un-commented.)



I preferred to declare i, j and k outside the loops, so that variables don't have to be re-created during each iteration.


SCJP 5.0 (100%)
saravanakumar rajendran
Greenhorn

Joined: Nov 14, 2005
Posts: 13
for (int k=1;k<=5;k++)
{for(int z=6-k;z<5;z++)
{
System.out.print(" ");
}
for (int l=k+1;l<=5 ;l++ )
{
System.out.print("*");
}
System.out.println();
}
thanks for your kind reply, and this is my answer
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Originally posted by Abdul Rehman:
[QB]Hi to all.
I am new to JavaRanch. I am planning to take the SCJP exam (CX-310-055) by the end of Decemeber. I usually come here to see other people's problems, questions, etc. & try to solve them on my own. This helps me check my concepts in various fields.
I read the first post and made out the solution in just 2 minutes
Here is the code ....


Hi Abdul,

welcome to the Ranch!

Good job on solving the OPs (Original Posters) problem in 2 mins

General remark:

Please remember that this is the beginners forum and it's therefore important
that the OP'ers work out their solution (the techinical one, that is) by
themselves. Hints, ideas and pseudo code is to be prefeered over java code
in other words.

Now.. the OP seems to have solved the problem already, so posting code here
is okay... nevertheless I find it important to stress the importance of
the above


/Svend Rost
[ November 10, 2006: Message edited by: Svend Rost ]
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
@Svend Rost

Thank you very much for your advice. I will take care in the future.
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
I preferred to declare i, j and k outside the loops, so that variables don't have to be re-created during each iteration.

Suppose you have a loop like this:

for(int i = 0; i < 10; ++i)

If i was recreated during each interation, how would the ending condition ever be reached?

This helps me check my concepts in various fields.

Do some more reading about for-loops.
[ November 10, 2006: Message edited by: sven studde ]
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
Originally posted by sven studde:

Suppose you have a loop like this:

for(int i = 0; i < 10; ++i)

If i was recreated during each interation, how would the ending condition ever be reached?


In the problem under discussion, we have nested looping. There is no problem with i, rather, j & (possibly) k would be re-created during each iteration of the outer loop. That's what I meant. One can declare i in the for-loop header; there's no problem with i.

Best regards,
Abdul Rehman.
Rahesh Kumar
Ranch Hand

Joined: Sep 10, 2004
Posts: 53
Please check this out.Quite simpler

String start = "*****";
StringBuffer buffer = new StringBuffer();
buffer.append(start);
System.out.println(buffer.toString());
for(int l=1;l<buffer.length();l++)
{
buffer = buffer.replace(l-1,l,"s");
System.out.println(buffer.toString());
}
Thanks,
Rahesh
red rived
Greenhorn

Joined: Nov 11, 2006
Posts: 5
--->> this is your main
for (int i=0; i<totalNumberOfChar; i++) {
PrintChar(i, " ");
PrintChar(totalNumberOfChar-i, "*");
System.out.println();
}

--->> this is for PrintChar function or whatever you call this in java
Function: PrintChar
accepts: (int totalNumberOfChar, char charToPrint)

for (int i=0; i<totalNumberOfChar; i++) {
System.out.print(charToPrint);
}



i am also new in java would like to help too..
[ November 12, 2006: Message edited by: red rived ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding logic on for loop