Results 1 to 11 of 11
Thread: Palindrom - method charAt()
- 11-14-2008, 02:02 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
Palindrom - method charAt()
Hi,
hope some of you may help me out. Im frustrated with this one:
public static boolean isPalindrome(String str){
boolean bvalue = true;
int i = 1;
int j = str.length();
do {
if (str.charAt(i)==str.charAt(j)){
bvalue = true;
}
else {
bvalue = false;
}
i++;
j--;
} while (i<=j);
return bvalue;
}
problem:
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 #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 13
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.
- 11-14-2008, 02:32 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 8
- Rep Power
- 0
at the first time,
j=str.length(),
you call str.charAt(j).
public static boolean t(String str){
boolean bvalue = true;
int i = 0;
int j = str.length()-1;
do {
if (str.charAt(i)!=str.charAt(j)){
bvalue = false;
}
i++;
j--;
} while (i<=j);
return bvalue;
}
- 11-14-2008, 03:21 AM #4
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
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 #5
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 13
^ 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?
- 11-14-2008, 11:19 AM #6
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
boolean bvalue = true;
int i = 0;
int j = str.length()-1;
if (j==-1) {
bvalue = false;
}
do {
if (str.charAt(i)!=str.charAt(j)){
bvalue = false;
}
i++;
j--;
} while ((i<=j)&&(j>=0));
return bvalue;
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 #7
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.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-14-2008, 02:49 PM #8
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
Thank you all. I put Palindrom+ java into google search and found the answer to all my questions.
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)){
return false;
}
left++;
right--;
}
return true;
- 11-15-2008, 04:33 PM #9
You have not done all the work directed by CJSL.
I suggest reading Cay S. Horstmann - a noted author on Java topics.Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 11-16-2008, 03:58 AM #10
Sad...
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...
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-16-2008, 05:37 AM #11
Similar Threads
-
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 06:12 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 08:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 09:37 AM -
client/server palindrom
By Rgfirefly24 in forum New To JavaReplies: 0Last Post: 04-28-2008, 03:19 AM -
Help With Input.charAt(LastIndex);
By susan in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 05:22 AM
Bookmarks