java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:687)
at Wrapper.isPalindrome(Wrapper.java:9)
at Evaluator.main(Evaluator.java:28)
at EvaluatorExecutor.main(EvaluatorExecutor.java:7)

This method is supposed to compare words like "anna", whether they are symetric or not like "nana" or any other chars like ". I tried i=1 and j=1 for example, because index for charAt() is 0 to length()-1. Still the same error.
Pls. give me some advice. Thank you very much.

11-14-2008, 02:20 AM

emceenugget

What happens if you are given an empty string as input?

P.S. Revert to 0...n-1 and not 1...n, I know you know that it was correct that way.

java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:687)
at Wrapper.isPalindrome(Wrapper.java:9)
at Evaluator.main(Evaluator.java:28)
at EvaluatorExecutor.main(EvaluatorExecutor.java:7)

This is what the compiler tells me, after i changed it as you advised above. the problem is the following. if str="" and we have j=-1 because str.length=0, then it is undefined for charAt() method. Argument for charAt() is in range from 0 to length-1. So how do I change it?

11-14-2008, 03:31 AM

emceenugget

^ That's what I was hoping you could do on your own... You know the cause of the problem, you should find the solution.

Think about this:
Is an empty string a palindrome? Are all empty strings palindromes? Does it even matter, as in, should you even bother checking if empty strings are palindromes?

An empty string is no palindrome obviously. all empty strings are no palindrome. I would not put my questions into a forum for java learning beginners, if I knew how to fix it in java. Since j cannot be -1 as an argument for j i put an extra j>=0 into the while loop condition and and if conditio for j=-1, because an empty string is no palindorme, thats why its false. Still I get this error message. Really thank you for your advise emceenugget, I appreciate it. Could you now please tell me how I fix the code? I posted the source code so someone could help me with direct answers and no additional questions. I asked it myself before and tried it, but didnt work. So whats to change?

11-14-2008, 02:27 PM

CJSLMAN

Suggestions...

If you know the word length is zero (j=-1), then why continue with the rest of the code? ... return inmediately with bvalue=false. As a matter of fact, the moment you find out that there is not an equivalency any place in your code, you should return bvalue= false.

In your comparation, (str.charAt(i)!=str.charAt(j)), don't use "!="... use the .equals method or some other comparative method.

Have you taken into account uppercase and lowercase letters?

Add println() to your code so you can follow the flow and see what's happening. Once the problem is found, just take the printllns.

Well, it's very sad to give advise and have it totally blown away by what was found on Google. The OP doesn't even know (because he didn't read my post carefully) that the solution he found will only work some times.
It's sorta scary to see the quality of some of the OPs going through this forum... would you get in an airplane that had sw programmed by these future "programmers" (including the one offering payment to get his hoemwork done).
Sad...

CJSL

11-16-2008, 05:37 AM

fishtoprecords

Quote:

Originally Posted by CJSLMAN

Well, it's very sad to give advise and have it totally blown away by what was found on Google.

Very true. Googling for an answer to what is clearly homework is a sin.