1

I'm not really sure why I am getting this error. The code is meant to test palindromes disregarding punctuation.

So here is my code:

            char junk;
            String temp = "";

            for (int i = 0; i < txt.length(); i++)
            {
                junk  = txt.charAt(i);
                if (Character.isLetterOrDigit(txt.charAt(jumk)))
                {
                    temp += junk;
                }
            }
            txt = temp;
            left = 0;
            right = txt.length() -1;

            while (txt.charAt(left) == txt.charAt(right) && right > left)
            {
                left++;
                right--;
            }

java.lang.StringIndexOutOfBoundException : String index out of range 0
at PalindromeTester.main(PalindromeTester.java:35)

and line 35 is as following:

    while (txt.charAt(left) == txt.charAt(right) && right > left)
2
  • As a side note: I'd replace the first for with: txt.replaceAll("\W", ""); docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/… Commented Oct 28, 2012 at 22:41
  • You almost had it... this line: Character.isLetterOrDigit(txt.charAt(yP)) is looking for a position in a string, not a character. Commented Oct 28, 2012 at 22:41

2 Answers 2

1
 if (Character.isLetterOrDigit(txt.charAt(yP)))

is your problem, yP is a char not a reference to a position.

What you probably meant was:

 if (Character.isLetterOrDigit(yP))

Edit: My comment: Well the value of right would be -1 and charAt would require a an integer greater than 0.. so you should check the length of txt and if it's == 0 then display a message saying an actual word is required.

You should stop execution before you get to this line:

right = txt.length() -1;

This is your fixed code:

do
    {
        System.out.println("Enter a word, phrase, or sentence (blank line to stop):");
        txt = kb.nextLine();
    }

while (!txt.equals(""));

    txt = txt.toLowerCase();
    char yP;
    String noP = "";

    for (int i = 0; i < txt.length(); i++)
    {
        yP  = txt.charAt(i);
        if (Character.isLetterOrDigit(txt.charAt(yP)))
        {
            noP += yP;
        }
    }
    txt = noP;

    left = 0;
    right = txt.length() -1;

    while (txt.charAt(left) == txt.charAt(right) && right > left)
    {
        left++;
        right--;
    }

    if (left > right)
    {
        System.out.println("Palindrome");
        cntr++;
    }
    else
    {
        System.out.println("Not a palindrome");
    }
Sign up to request clarification or add additional context in comments.

6 Comments

No worries, and welcome to stack overflow! If any of these answers helped you, you should click the checkbox beside their answer to indicate it as the accepted answer.
ok actually now it works for saying if it is a palindrome or not but when i just hit enter without putting anything i can another error on line 38 which is: while (txt.charAt(left) ==....... line same error as before
Well the value of right would be -1 and charAt would require a an integer greater than 0.. so you should check the length of txt and if it's == 0 then display a message saying an actual word is required.
yea what im meaning is in the do while loop i want it to somehow skip it it if they enter a blank line and tell them how many palindromes there are. I tried to do it with the do while loop but since it is asking them in the loop the while doesn't work until after.
See my edited post, and you'll see the closing of the while loop is what gets you.
|
0

The variable yP is your character at index i, not an index (as you are using it on the line giving you the error). Change that line to:

if (Character.isLetterOrDigit(yP)) { ...

EDIT FOR THE NEW PROBLEM:

You don't need a while loop to check if the user entered nothing, since you don't want to do something repeatedly in this case (which is what loops are for). Since you only want to do something once, i.e. print out how many palindromes they have found, you can just use an if statement. The structure would look like this:

do {
    get user input

    if they entered the empty string "" {

        print out how many palindromes they have found so far

    } else { // they must have entered text, so check for palindrome

        your normal palindrome checking code goes here

    }

} while (your condition);

EDIT 2:

Try changing

if (left > right)

to

if (left >= right)

Since if left==right, that means they are both on the median character in an odd-length string (e.g. the 'y' in kayak) which means the string is a palindrome.

20 Comments

ok actually now it works for saying if it is a palindrome or not but when i just hit enter without putting anything just to get it to say how many palindromes there were i get another error on line 38 which is: while (txt.charAt(left) ==....... line same error as before
Yeah. That's because if you don't enter anything, the program doesn't skip to your bottom while loop, it still runs the whole thing. So firstly, you'll try and access txt.charAt(left=0) when there are no characters in txt - that's causing the error. Secondly, programs don't 'remember' values after you quite them and run them again; if you want to know how many palindromes you found, you have to do that in the same session of the program running as you actually did the finding.
I'd talk to you in chat about this if it's easier.
no i do not mind but yea i just figured that out so how would i get it to still loop the question to them but also have it where the enter a blank it would skip thats what i dont know. Becuase the while loop isn't working until after the whole loop goes through and im not sure how to change that.
Oh wow my bad, didn't read your code properly! I'll put this in an edit on my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.