2016-06-08 2 views
0
package palindrome; 
import java.util.Scanner; 
public class Palindrome 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Enter the word = > "); 
     String word = input.nextLine(); 
     int flag = 0; 
     int x = word.length(); 
     int i = 0; 
     int j = x; 
     for(; i<=x/2 && j>=x/2; i++,j--) 
     { 
      if(word.charAt(i)!=word.charAt(j)) 
      { 
       flag = 1; 
       break; 
      } 
     } 
     if(flag==0) 
     { 
      System.out.printf("The word '%s' is a palindrome", word); 
     } 
     else 
      System.out.printf("The word '%s' is not a palindrome", word); 
    } 


} 

출력 비명 오류에 대한 코드 :

단어 => 부인을 입력은회문 (오류 아래)

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5 
at java.lang.String.charAt(String.java:658) 
at palindrome.Palindrome.main(Palindrome.java:16) 
+0

출력 비명! 시끄러운 일련의 바이트? – Mena

+0

나는 이것을 http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it의 속임수로 표시하려고한다. 그러나 그것은 구체적이지 않다. 'StringIndexOutOfBoundsException'로 ... –

+0

문자열에'x' 문자가 있으면 마지막 문자의 색인은'x'가 아니라'x-1'입니다. –

답변

1

당신 j의 초기 값을 길이 1로 설정해야합니다.

import java.util.Scanner; 
public class Scratch 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Enter the word = > "); 
     String word = input.nextLine(); 
     int flag = 0; 
     int x = word.length()-1; 
     int i = 0; 
     int j = x; 
     for(; i<=x/2 && j>=x/2; i++,j--) 
     { 
      if(word.charAt(i)!=word.charAt(j)) 
      { 
       flag = 1; 
       break; 
      } 

     } 
     if(flag==0) 
     { 
      System.out.printf("The word '%s' is a palindrome", word); 
     } 
     else 
      System.out.printf("The word '%s' is not a palindrome", word); 
    } 


} 
+0

'x'의 값을'word.length() - 1'로 설정하는 것이 아니라'j'의 값을'x-1'로 설정하는 것이 좋습니다. 다시, 루프 가드를'i

+0

코드가 주어지면 최소 변경으로 간단한 수정이 가능합니다. 나는 코드 맨을 보았다. 그런 사소한 일에 많은 변수를 사용하겠습니까? –

2
int j = x; 

은 다음과 같아야합니다

int j = x-1; 
-2

결과가 5로 나뉘어 2로 나뉘어 진다고 가정합니다. 중간 길이를 정수로 유지하십시오.

0

색인은 배열에서 0부터 시작합니다.

package palindrome; 
import java.util.Scanner; 
public class Palindrome 
{ 
public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 
    System.out.print("Enter the word = > "); 
    String word = input.nextLine(); 
    int flag = 0; 
    int x = word.length(); 
    int i = 0; 
    int j = x-1; 
    for(; i<=x/2 && j>=x/2; i++,j--) 
    { 
     System.out.println(i+" " +j); 
     if(word.charAt(i)!=word.charAt(j)) 
     { 
      flag = 1; 
      break; 
     } 
    } 
    if(flag==0) 
    { 
     System.out.printf("The word '%s' is a palindrome", word); 
    } 
    else 
     System.out.printf("The word '%s' is not a palindrome", word); 
} 

}

0
당신은 단지 1 단어의 길이를 감소하는 것을 잊지

: 자바가되지 않습니다 때문에 그래서 길이가 항상 1을 뺀 수 있어야 0에서 시작에 은 문자열과 배열의 인덱스를 기억 도달했습니다. 단어의 길이가 4 예를 경우 4.

int x = word.length()-1; 
    int i = 0; 
    int j = x; 
    for (; i <= x/2 && j >= x/2; i++, j--) { 
     if (word.charAt(i) != word.charAt(j)) { 
      flag = 1; 
      break; 
     } 
    } 
0

그냥 다른 답변 명확히하기의 총 길이하게 0 3 인 지수 3까지 얻을 수 있습니다 :
문자열은 문자의 배열입니다 당신이 "부인"

따라서
[m][a][d][a][m] -> is 5 letters, so its length is 5 
[0][1][2][3][4] -> but the indexes start with 0, so the last one is 4 

x=word.length()-1

0

쓰기, 그래서 만약, 당신의 코드에서 실수를 얻었다.

"값 j = x;" // is wrong String은 문자 배열이므로 0부터 string.length-1까지입니다.

따라서 int j = x-1; //

0

나의 제안은 당신이, 당신은 당신의 코드가 깨끗하고 code.You이 방법을 아래에 사용할 수있는 품질이 회문를 확인하기 위해이 방법 회문를 확인하는 seprate 방법을 가지고 있어야한다는 것입니다 잘 할 것 :

public static void main(String[] args) 
{ 
     Scanner input = new Scanner(System.in); 
     System.out.print("Enter the word = > "); 
     String word = input.nextLine(); 

     if(isPalindrome(word)) 
     { 
      System.out.printf("The word '%s' is a palindrome", word); 
     } 
     else 
      System.out.printf("The word '%s' is not a palindrome", word); 
} 


private static boolean isPalindrome(String input) { 
     if(input == null || input.isEmpty()) { 
      return false; 
     } 
     for(int i=0,j=input.length() -1; i > j; i++,j--) { 
      if(input.charAt(i) != input.charAt(j)) { 
       return false; 
      } 
     } 
     return true; 
}