This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested for loop

 
shuba gopal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi I am trying to check if the elements in a given array are Palindrome. The flow does not enter the second for loop( for(int j=b-1;j<=mid2;j--). Eclipse does not show any errors. Any ideas? Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest the liberal application of "System.out.println()" statements. Print out what mid1 is just before you hit your outer loop, to see what it is actually looping over.

print what b-1 and mid2 are just before you enter your second loop to see what it is actually looping over.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shuba,

As per my point of view if possible avoid nested loop that will optimized performance of code

I written below code for palindrome in that I didn't used nested loop. You can refer below code.



Let see others members's opinion what they will suggest.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shuba gopal wrote:Hi I am trying to check if the elements in a given array are Palindrome. The flow does not enter the second for loop( for(int j=b-1;j<=mid2;j--). Eclipse does not show any errors. Any ideas? Thanks

Yes. Look at that loop test again. What is your inner loop doing?

BTW, you don't need nested loops; it can all be done in one (and the length being odd or even is actually a red herring).

Winston

 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kulkarni wrote:I written below code for palindrome in that I didn't used nested loop...

I'm relatively new here, so I'm not up on all the etiquette, but most of the forums that I belong to frown on full-code solutions. Far better to steer OP in the right direction and let the lightbulb come on by itself.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kulkarni wrote:
I written below code for palindrome in that I didn't used nested loop. You can refer below code.

Please don't provide full solutions like this. We want people to learn to code for themselves. Handing them a fully implemented answer doesn't really help them learn, just like handing someone a fish doesn't help them eat tomorrow.
 
shuba gopal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred,

I had used many System.out.println() statements. I had removed all of them just before posting here because they made the code look more complicated. That is how I found that the flow does not enter the second loop.

For the second loop, for(int j=b-1;j==mid2;j--) - b-1 = 5 and j=3. SOP just inside this loop is not printed. I just wanted to confirm if my usage is correct. Thanks Shuba
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:...most of the forums that I belong to frown on full-code solutions. Far better to steer OP in the right direction and let the lightbulb come on by itself.

Winston

as you can see, the moderators (especially me) agree with this 100%. In fact, it is even one of the points on our HowToAnswerQuestionsOnJavaRanch FAQ.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:...In fact, it is even one of the points on our HowToAnswerQuestionsOnJavaRanch FAQ.

So I notice.

BTW, I think the fact that those things are automatically linkable is great. Wish OTN did it.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shuba gopal wrote:For the second loop, for(int j=b-1;j==mid2;j--) - b-1 = 5 and j=3. SOP just inside this loop is not printed. I just wanted to confirm if my usage is correct. Thanks Shuba

in the original code sample you posted, you have

for(int j=b-1;j<=mid2;j--)

but in your prior post, you say it is

for(int j=b-1;j==mid2;j--)

Which is correct?

 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Please don't provide full solutions like this. We want people to learn to code for themselves. Handing them a fully implemented answer doesn't really help them learn, just like handing someone a fish doesn't help them eat tomorrow.

Hi fred,
I agree with you and I will take care for the same in future.
Thanks for feedback
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shuba gopal wrote:For the second loop, for(int j=b-1;j==mid2;j--) - b-1 = 5 and j=3. SOP just inside this loop is not printed. I just wanted to confirm if my usage is correct. Thanks Shuba

Doesn't look like it to me. As I said before: What is you inner loop trying to do? If you think about that I suspect you'll get the answer.

Furthermore, I think you might do well to step back from your code and look at the original problem again (never a bad idea, even for us old hands).

What is a palindrome? What are its properties? If you start at the first character, what should it equal? And the 2nd? And the 3rd?.... Draw it out on a piece of paper and try it.
Do you see any pattern?

If so, write it down in English (or your native language) and then try to write a program based on it.

Winston

 
shuba gopal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred, Ninad, Winston, thank you very much for responding to my question.

Winston, I finally figured out that the same logic applies when the number of elements are odd or even. I cant believe I missed this. Writing it out does clear things up. Many thanks.


 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shuba gopal wrote:Winston, I finally figured out that the same logic applies when the number of elements are odd or even. I cant believe I missed this. Writing it out does clear things up. Many thanks.

And very well done to you. I bet you're pleased with yourself, and rightly so: a very nice, clear piece of code.

A little teaser for you: There are two things you could do to make it even tighter (and one of them will actually make it run faster too).
Can you spot what they are?

Winston
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also reformat that code. { and } on lines by themselves. Single spaces before and after binary operators. It makes it much easier to read. Also use spaces, not tabs, for indenting.

Also try writing a recursive palindrome method
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You should also reformat that code. { and } on lines by themselves.

Unless the style guide you are adhering to (correctly ) says that the { should be on the same line as the for, while, do, etc statement.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I’m not letting you have the last word, Joanne Neal. Not even if I know you are right!
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I’m not letting you have the last word...

Actually, I'm with Joanne on that one. I generally stick me opening braces at the end of the line unless space dictates otherwise (and being an old COBOLer, I use column 72 as my limit ).

As for those guidelines, I don't agree with most of section 3 (particularly 3.1.2, which is plain wrong in my view), and also 2.3. I always make my constants all-caps - particularly if they're public - and the reasoning is bad too: when is a field called 'PI' likely to be refactored not to be a constant?.

However, all grist for the programming discussion mill .

Winston

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I switched from 80 to 120 character maximums recently, because 80 made me jump through hoops at times to get a proper looking layout while still keeping under 80. 120 looked well enough on my screen. I agree on (most of) section 3 being rubbish.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did say Joanne was right. She and I have been winding each other up for ages.

We had a block of code unrelieved by spacing (except a blank line after one of the ifs) and it was simply difficult to read. Because you can’t see which } terminates the for, you cannot tell that the continue has no effect. I personally think the best convention about {} is what Cai Horstmann says, something like, “braces must match each other either vertically or horizontally.”

And I take no notice of bits in that style guide about capital letters for constants, not using do, not including i++ in expressions, etc. Agree about restricting column counts; we see too many bits of code here where you have to scroll right and left to read it. I usually add new lines to such code, but sometimes they use tabs for indenting, and that can make it look even worse after my editing
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Section 3 appears to be what I have just said I take no notice of.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shuba gopal wrote: . . . Winston, I finally figured out that the same logic applies when the number of elements are odd or even. . . .


You realise that you only need to traverse half an array to find out whether it is palindromic? If arr[0] == arr[arr.length - i], then arr[arr.length - i] == arr[0].
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:I switched from 80 to 120 character maximums recently, because 80 made me jump through hoops at times to get a proper looking layout while still keeping under 80. 120 looked well enough on my screen. I agree on (most of) section 3 being rubbish.

Ah, you're fond of reallyReallyDescriptiveAndDetailed() method/class/field names are you? Or do you like Builders?

Actually, me too (both); but I try to keep it to a dull roar .

Winston
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I did say Joanne was right. She and I have been winding each other up for ages...

Yeah, I know. I was just joining her club.

BTW, the one major piece of advice that I think should be in every guideline on coding is conspicuously absent from that one:
Keep your methods short.

Other than Swing coding (which I loathe), I can't remember the last time I wrote a method that was more than about 60 lines long; and if I do, it's usually an indication that I haven't broken the problem down enough.

Winston
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Campbell Ritchie wrote:I’m not letting you have the last word...

Actually, I'm with Joanne on that one. I generally stick me opening braces at the end of the line unless space dictates otherwise

Well actually I disagree with that. Consistency is more important than where the { actually goes. If you need to split a line don't put just the { on a new line if you normally put it on the same line. Split the line somewhere else.

Thanks for posting that though. It means I (currently) have the last word on the subject
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:It means I (currently) have the last word on the subject

Not any more. I wrote a little ditty on this subject a while ago. Hope you don't mind if I share:

Newlines are for namby-pambies
Indentation too
Spaces for the faint of heart
So which of those are you?
Real coders know the score
that nothing could be finer
than to condense all their thought
into a one-liner.

Winston
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't give up the day job
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Don't give up the day job
I won’t.

But there is a school of thought, including many older C programmers (and FORTH programmers), who believe a program should occupy one line. If you can get the entire program into the () after for, so much the better.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Rob Spoor wrote:I switched from 80 to 120 character maximums recently, because 80 made me jump through hoops at times to get a proper looking layout while still keeping under 80. 120 looked well enough on my screen. I agree on (most of) section 3 being rubbish.

Ah, you're fond of reallyReallyDescriptiveAndDetailed() method/class/field names are you? Or do you like Builders?

Actually, me too (both); but I try to keep it to a dull roar .

Winston

The biggest problem is with long class names:
That's already 93 characters, and that's an easy example. Also, generics can have a big impact on line length. A method with generic type <T extends Comparable<? super T>> already required 33 characters for the type; if that method is public static that's already 51 characters including leading indentation of 4 spaces. That leaves very little space for the return type, method name and parameters.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . .
BTW, the one major piece of advice that I think should be in every guideline . . .
Keep your methods short. . . .
More a design principle than style, but I agree. I read somewhere (forget where) that a method should be less than half a page, and a method which goes over a whole page really ought to be refactored.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say, we're all going quite off-topic here. Can we get back to the palindrome issue, and have the code style discussion somewhere else?
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. I do have a tendency to drift off topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic