2014-02-24 1 views
0

나 자신이 만든 텍스트 파일에서 읽음으로써 배열을 만드는 작업을했습니다. 개념 상 프로그램은 내가 입력 한 단어를 읽고 배열에 저장한다고 가정합니다. 다음으로, 재귀 적 메소드를 작성하여 각 라인이 회문인지 확인하고 결과를 인쇄합니다. 현재 스택 오버플로 오류가 발생합니다. 죄송합니다 코드가 너무 많이에 대한 주석을 경우. FindPalindrome 방법은 문자열의 원래 배열의 첫 번째 항목을 살펴보고 그것의 매개 변수를 무시 이후 FindPalindrome 방법에 재귀 호출에배열에서 읽기 및 재귀 메서드를 사용하여 Palindromes 확인

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

    } 

} 
}  
+1

'end while statement'가 필요할 수도 있지만 절대 사용해서는 안됩니다. 들여 쓰기를 어떻게 든 할 수 있도록 코드를 포맷해야합니다. '// int 변수를 0으로 설정하지 않아도된다. '라는 라인은 자명하다. 성명서의 목적이 무엇인지 논평하십시오. – user60561

답변

2

, 당신은, 당신이 사용하지 않을 문자열을 전달하고 있습니다. 스택을 오버플로 할 때까지 동일한 인수로 반복적으로 호출합니다. 모든 곳 FindPalindrome에서 당신이 Palindromes[i] 참조하는

, 당신은 가능성이 매개 변수 FoundPalindrome 대신 참조해야하고 실제로 작동 할 수 있습니다.

+0

저는 매우 늦었지만 당신의 솔루션이 효과가있었습니다. 감사! –