-1

I am to create a program that checks for palindromes in a sentence and display the palindromes that have been found. My following code keeps giving me a "String is out of bounds" error. What am i doing wrong?

My Program:

import javax.swing.JOptionPane;

public class Palindromechkr {
    public static void main(String[] args) {
        //Declare Variables
        String Palin, input, Rinput = "";
        int wordlength, spacePos;
        //Ask for sentance
        input = JOptionPane.showInputDialog("enter a sentance");
        //Split string
        spacePos = input.indexOf(" ");
        String word = input.substring(0, spacePos);
        //Get palindromes   
        System.out.println("Your Palindromes are:");
        for (int counter = 0; counter < input.length(); counter++) {
            //Reverse first word
            wordlength = word.length();
            for (int i = wordlength - 1; i >= 0; i--) {
                Rinput = Rinput + word.charAt(i);
                //Add word to An array of Palindromes 
                if (Rinput.equalsIgnoreCase(word)) {
                    Palin = word;
                    System.out.println("Palin:" + Palin);
                    break;
                }
                //Move on to the next word in the string
                input = input.substring(input.indexOf(" ") + 1) + " ";
                word = input.substring(0, input.indexOf(" "));
            }
        }
    }
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Waazzaap
  • 13
  • 4
  • Strings are really just an array of chars. So if you're getting a 'string out of bounds' error its because you are trying to access an invalid element in the char array. – nullByteMe Mar 12 '15 at 15:46
  • 2
    spacePos is -1 if you enter one word! do you use debugger? can you help a lot... – sgpalit Mar 12 '15 at 15:46
  • Hey, thanks for the response. Ive just started programming not to long ago (I use net beans and i believe its got a debugger) but im not sure how to use a debugger. I will handle the -1 issue if one word is entered after. I would like to get the program checking through a sentence first. If you have any suggestions feel free :) – Waazzaap Mar 12 '15 at 15:58
  • possible duplicate of [Check string for palindrome](http://stackoverflow.com/questions/4138827/check-string-for-palindrome) – Bassdrop Cumberwubwubwub Mar 12 '15 at 15:58
  • @inquisitor I am not sure how to fix that – Waazzaap Mar 12 '15 at 16:04

2 Answers2

0

You should have done is

public static void main(String[] args) {

      String Palin, input, Rinput = "";
      input = new Scanner(System.in).nextLine();
      //Split string
      for(String str : input.split(" ")){
         for (int counter = str.length()-1; counter >= 0; counter--) {
            Rinput = Rinput + str.charAt(counter);
         }
         if (Rinput.equalsIgnoreCase(str)) {
            Palin = str;
            System.out.println("Palin:" + Palin);
         }
         Rinput="";
      }
   }

I don't know if you are aware but StringBuilder has a reverse method to it. Which can be used like this :

public static void main(String[] args) {
      String input;
      input = new Scanner(System.in).nextLine();
      //Split string
      for(String str : input.split(" ")){
         if (new StringBuilder(str).reverse().toString().equalsIgnoreCase(str)) {
            System.out.println("Palin:" + str);
         }
      }
   }
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
  • we have not looked into string builder yet in my course yet, I appreciate your help tho, Thanks. – Waazzaap Mar 12 '15 at 16:18
  • Would you actually be able to explain what is happening in that for loop for me/How it works? for(String str : input.split(" ")) – Waazzaap Mar 12 '15 at 16:40
  • When you do input.split(" ") it return you and array of strings which are operated on spaces. EG: if input is "123 abc" it will return an array of 2 strings arrString[0]="123" & arrString[1]="abc". – StackFlowed Mar 12 '15 at 17:47
0

If you know functions you can use a recursive function to build the palindrome version of a string (it's a common example of how recursion works).

Here's an example:

import javax.swing.JOptionPane;

public class Palindromechkr {
    public static void main(String[] args) {
        //Declare Variables
        String Palin, input, Rinput = "";
        int wordlength, spacePos;
        //Ask for sentance
        input = JOptionPane.showInputDialog("enter a sentance");

        String[] words = input.split(" +"); // see regular expressions to understand the "+"
        for(int i = 0; i < words.length; i++) { // cycle through all the words in the array
            Rinput = makePalindrome(words[i]); // build the palindrome version of the current word using the recursive function

            if(Rinput.equalsIgnoreCase(words[i])) {
                Palin = words[i];
                System.out.println("Palin: " + Palin);
            }
        }
    }

    // this is the recursive function that build the palindrome version of  its parameter "word"
    public static String makePalindrome(String word) {
        if(word.length() <= 1) return word; // recursion base case

        char first = word.charAt(0); // get the first character
        char last = word.charAt(word.length()-1); // get the last character
        String middle = word.substring(1, word.length()-1); // take the "internal" part of the word
                                                            // i.e. the word without the first and last characters

        String palindrome = last + makePalindrome(middle) + first; // recursive call building the palindrome
        return palindrome; // return the palindrome word
    }
}
Antiphon0x
  • 197
  • 1
  • 10
  • note that my function actually reverts the word characters, while the definition of palidrome is different, so I would probably have called the function something like "revertString" – Antiphon0x Mar 13 '15 at 14:08