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)
Fd Fehfhd
  • 23
  • 4
  • As a side note: I'd replace the first for with: txt.replaceAll("\W", ""); http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html#sum – ignis Oct 28 '12 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. – ajacian81 Oct 28 '12 at 22:41

2 Answers2

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");
    }
ajacian81
  • 7,419
  • 9
  • 51
  • 64
  • 1
    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. – ajacian81 Oct 28 '12 at 22:44
  • 1
    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 – Fd Fehfhd Oct 28 '12 at 22:51
  • 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. – ajacian81 Oct 28 '12 at 22:55
  • 1
    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. – Fd Fehfhd Oct 28 '12 at 23:00
  • See my edited post, and you'll see the closing of the while loop is what gets you. – ajacian81 Oct 28 '12 at 23:04
  • yea that works except i need to have it tell them if its a palindrome or not right after they enter it. – Fd Fehfhd Oct 28 '12 at 23:09
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.

chm
  • 1,519
  • 13
  • 21
  • 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 – Fd Fehfhd Oct 28 '12 at 22:55
  • 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. – chm Oct 28 '12 at 23:02
  • I'd talk to you in chat about this if it's easier. – chm Oct 28 '12 at 23:03
  • 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. – Fd Fehfhd Oct 28 '12 at 23:06
  • Oh wow my bad, didn't read your code properly! I'll put this in an edit on my answer. – chm Oct 28 '12 at 23:08
  • Ok now it works but after it says You found "" palindromes i get a stringindexoutofboundsexcepton right after wards on another line still with that same while loop as before. while (txt.charAt(left) == ..... – Fd Fehfhd Oct 28 '12 at 23:25
  • Are you sure that section of code is in an 'else' block? It should be: if empty string { ... } ELSE { all palindrome code }. – chm Oct 28 '12 at 23:28
  • ok it works except there one palindrome that is not working that is supposed to work: Never odd(5,7) or even(4,6) oh and thanks for all the help man; is that going to take a lot to change because i just found out it needed to work with that. – Fd Fehfhd Oct 28 '12 at 23:33
  • Yeah, edited with a problem I saw before that might help. And no problem! – chm Oct 28 '12 at 23:38
  • It's also probably worth looking at the answer from this SO question, they are using the same kind of logic as you: http://stackoverflow.com/questions/4138827/check-string-for-palindrome – chm Oct 28 '12 at 23:41
  • Ok its still not working i put "Never odd(5,7) or even(4,6)" into the palindrome tester and it is supposed to work. – Fd Fehfhd Oct 28 '12 at 23:43
  • What? That string's not a palindrome. If your program says it's not a palindrome, your program is correct. – chm Oct 28 '12 at 23:44
  • ok i think i may have figured it out thanks for all the help if i have anymore questions i will come to you, since you seem very knowledgeable. Thanks. – Fd Fehfhd Oct 28 '12 at 23:55
  • ok i'm not sure if you will respond since this is a little late but im getting an error when i only put punctuation in. Is there a way to fix that and just say its not a palindrome – Fd Fehfhd Oct 29 '12 at 02:00
  • Yes, do your loop that filters out all the punctuation *before* you check if the string is empty. I probably should have mentioned that before actually. – chm Oct 29 '12 at 02:04
  • I tried it and it didn't work basically i want it when a user enters ,,,,,,,,,,, it says no its not a palindrome but im just getting in out of bound exception. – Fd Fehfhd Oct 29 '12 at 02:29
  • i guess i should post a new question maybe. – Fd Fehfhd Oct 29 '12 at 02:52
  • You still have the 'while (txt.charAt(left)...' bit at a place in the code where empty strings can get. All code that involves accessing characters in the string MUST be in a section that empty strings can't get to, e.g. if (string is empty) { do something } else { *here is ok to access string chars* } – chm Oct 29 '12 at 02:53
  • ok i got it to work except now it just ends the program because it is thinking the user just entered a blank line and so it says Not a palindrome and then it ends the program. – Fd Fehfhd Oct 29 '12 at 03:14
  • If you want something to happen repeatedly, you have to put it in a loop. The if/else structure I mentioned should go inside the larger loop of: while (user has input) { get input; if/else section; } – chm Oct 29 '12 at 03:22