• 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

Palindrome no. problem

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above program trying to show wheather any character is palindrome or not by having an input at command prompt. But when I entered an input radar, it is showing me an outofbound Exception.


why and how can I get my desire result?
 
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

The above program trying to show wheather any character is palindrome or not by having an input at command prompt. But when I entered an input radar, it is showing me an outofbound Exception.



The stackstace for the out of bounds exception, not only tells you the exact method and line number where it is happening -- it also tells you the index as well. This way, you can tell if you overflowed or underflow.

But in your case, are you sure you actually want to deference the array? Shouldn't you be checking start? not e[start]?

Henry
[ December 16, 2008: Message edited by: Henry Wong ]
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henery. Now I'm getting my desire result.
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More of a common issue, moving to appropriate forum.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One remark on how to slightly improve your algorithm:

you only need to go half-way. After you have hit the half-way point you will compare the same elements again. For instance, the last comparison is that of e[e.length - 1] and e[0] - but you've already compared e[0] with e[e.length - 1].

So the check can become
Should e.length be odd, this will miss one element but that is one element that you would compare with itself.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your condition should be


Just a piece of advice, you don't need to traverse the whole array. As you are moving from both sides each traversing the halves from both end. Your loop should run half way only i.e round(e.length/2)+1, because anyways after length/2 it is going to compare the same elements
For example
RADAR
it checks
R=R --> f=0,l=4
A=A --> f=1,l=3
D=D --> f=2,l=2
A=A --> f=3,l=1 ---> useless
R=R --> f=4,l=0 ---> useless

so you just need round(5/2)+1 = 3 iterations
 
Marshal
Posts: 79278
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sunil Kumar:
so you just need round(5/2)+1 = 3 iterations



You don't need 5 / 2 + 1; you need 5 / 2. The middle element will probably not differ a lot from itself.
 
Sunil Kumar
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh.. thanks for correcting that. I just missed it!!
You shouldn't have used the word "probably" . That was funny.
 
Campbell Ritchie
Marshal
Posts: 79278
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was quite right to say "probably"; I then know whether you are awake
 
reply
    Bookmark Topic Watch Topic
  • New Topic