Results 1 to 11 of 11
  1. #1
    user is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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.

  3. #3
    luckacc is offline Member
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Default

    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;
    }

  4. #4
    user is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    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?

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    ^ 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?

  6. #6
    user is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    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?

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    user is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    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;

  9. #9
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default

    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

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Quote Originally Posted by CJSLMAN View Post
    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.

    OP deserves an F for lacking the will to learn

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  3. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM
  4. client/server palindrom
    By Rgfirefly24 in forum New To Java
    Replies: 0
    Last Post: 04-28-2008, 02:19 AM
  5. Help With Input.charAt(LastIndex);
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 04:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •