Alexander Rivera
Alexander Rivera

Reputation: 25

Reading from Array and using Recursive method to check for Palindromes

I've been tasked with creating an array by reading from a text file created by myself. The program in concept, is supposed to read the words i entered, and store them in an array. Following, I am to create a recursive method to check and see if each line is a palindrome and print the result. Currently, I'm getting a stack overflow error. Sorry if the code is not commented on too much.

package palindrome;

import java.util.*;
import java.io.*;

/**
 *
 * @author alexanderrivera
 */
public class Palindrome {

    // Static variables can be used in ALL instances of a class
    public static String [] Palindromes;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {

    // Establishes a value to Palindromes   
    Palindromes = new String[10];
    int i=0;
    // calls the readFile method to read the file
    readFile("Palindrome.txt");
   FindPalindrome(Palindromes[i]);


 }// end of main method

    // begining of the readFile method
    public static void readFile(String fileName)
    {
        // sets the int variable to zero
        int i = 0;

        // establishes the java input for reading the file
        java.io.File inputFile = new java.io.File(fileName);

        // being try  catch block
        try{

         // establishes instance of the scanner to read the file
         Scanner fileReader = new Scanner(inputFile);

        // being while statement 
        while(fileReader.hasNextLine())
        {
        Palindromes[i] = fileReader.nextLine();

        i++;


        }// end while statement 

        // closes the file reader
        fileReader.close(); 

        // print the index to see if it was read


        }catch (FileNotFoundException e){
            System.out.println("Sorry, cannot find the file");
        }// end error message

  } // end the method


 public static void FindPalindrome(String FoundPalindrome) {


     int i=0;

   {    
   if (Palindromes[i].length() == 0 || Palindromes[i].length() == 1) {

            System.out.println(Palindromes[i] + " This is a Palindrome\n");
        }
        if (Palindromes[i].charAt(0) == Palindromes[i].charAt(Palindromes[i].length() - 1)) {

            FindPalindrome(Palindromes[i].substring(1, Palindromes[i].length() - 1));

        }
        // otherwise
        System.out.println("This is not a Palindrome \n");

    }

}
}    

Upvotes: 0

Views: 1460

Answers (1)

Don Roby
Don Roby

Reputation: 41137

In recursive calls to your FindPalindrome method, you're passing strings which you never use, since the FindPalindrome method looks at the first entry in the original array of strings and ignores its parameter. So it just repeatedly calls itself with the same argument until it overflows the stack.

Everywhere in FindPalindrome that you reference Palindromes[i], you should likely be referencing instead the parameter FoundPalindrome, and then it might actually work.

Upvotes: 2

Related Questions