简体   繁体   中英

String Index Out Of Bound Exception error

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

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 , ie 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 (eg the 'y' in kayak) which means the string is a palindrome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM