This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding logic on for loop

 
saravanakumar rajendran
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
...

 
saravanakumar rajendran
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot man.
 
saravanakumar rajendran
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that api is technically advance, i need it for beginners level.
any way thanks a lot for your kind reply
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys, i got it now
 
Abdul Rehman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
saravanakumar rajendran
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Svend Rost

Thank you very much for your advice. I will take care in the future.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--->> 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic