2013-10-05 5 views
-1

나는 프로그래밍을 배우려고 노력하는 아마추어입니다.간단한 Palindrome 호

저는 회문을하려고합니다. 그러나 코드에 오류가 있습니다.

public class Palindrome { 

    public static void main(String args[]) 
    { 
    String pal = "abc"; 

    public static void check(String pal) 
    { 
     if(pal==null) 
     { 
      System.out.println("Null Value..Exit"); 
     } 
     else 
     { 
      StringBuilder str= new StringBuilder(pal); 
      str.reverse(); 
      System.out.println(str.reverse()); 
     } 
    } 
    } 

} 

나는 어디로 가고 있습니까? 죄송합니다. 프로그래밍을 처음 접해 보았습니다. 그냥 배우려고!

+5

Java에서 함수를 중첩 할 수 없습니다. –

+1

먼저 코드를 작성하기 전에 프로그램이 수행해야하는 작업을 중단하고 생각하십시오. 어떤 단계를 밟아야합니까? 그 결과는 무엇이되어야 하는가? 어떻게 구성되어야합니까? 코드를 작성하기 전에 계획을 세우십시오. – Jesper

+1

오류가 발생하면 가장 먼저해야 할 일은 메시지가 무엇이 잘못되었는지 그리고 어디에 표시되는지를 읽는 것입니다. 당신이 그것을 이해하지 못한다면, 게시하십시오, 우리는 초자연적 인 마법사가 아니기 때문에, 또한 오류 메시지가 우리를 도웁니다. –

답변

3

당신은 당신의 코드에서 다음과 변경해야합니다.

public static void main(String args[]) { 
    String pal = "abc"; 
    check(pal); // Nested methods are not allowed, thus calling the check 
       // method, which is now placed outside main 
} 

public static void check(String pal) { 
    if (pal == null) { 
     System.out.println("Null Value..Exit"); 
    } else { 
     StringBuilder str = new StringBuilder(pal); 

     // I think I confused you by doing the below. 
     // str = str.reverse(); // commenting this 

     str.reverse(); // adding this 
     // str.reverse(); reverse will affect the str object. I just assigned it back to make it easier for you 
     // That's why if you add a SOP with str.reverse, it'll reverse the string again, which will give the original string back 
     // Original string will always be equal to itself. that's why your if will always be true 
     // give a SOP with str.toString, not reverse. 

     // str.toString is used because str is a StringBuilder object and not a String object. 
     if (pal.equals(str.toString())) { // if the string and its reverse are equal, then its a palindrome 
      System.out.println("Palindrome"); 
     } else { 
      System.out.println("Not a Palindrome"); 
     } 
    } 
} 
+0

와우. 이것은 얻은만큼 상세합니다! 나에게 자바 기초를 배우기에 좋은 곳을 제안 해 줄 수 있습니까? –

+0

@RockyBalBoa - 여기 있습니다. [Java 기초 자습서] (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/). – SudoRahul

+1

@RockyBalBoa - IDE를 사용하기를 권합니다. 사소한 구문 오류를 수정하고 컴파일 문제를 피하는 데 도움이됩니다. 논리적 인 문제는 항상 여기에 다시 올 수 있습니다 :) – SudoRahul

2

다른 메소드 안에 하나의 메소드를 쓸 수 없습니다.

public class Palindrome { 

    public static void main(String args[]) 
    { 
    String pal = "abc"; 
check(pal); 

    } 
    public static void check(String pal) 
    { 
     if(pal==null) 
     { 
      System.out.println("Null Value..Exit"); 
     } 
     else 
     { 
      StringBuilder str= new StringBuilder(pal); 
      str.reverse(); 
      System.out.println(str.reverse()); 
     } 
    } 
} 
+0

정말 고마워요! –

+1

@javaBeginner이 함수 호출을'check (pal);'와 같이 추가해야합니다. – exexzian

0
public static boolean checkPalindrome(String str) { 
    return str.equalsIgnoreCase(new StringBuilder(str).reverse()); 
} 
0
private static boolean isPalindrome(String str) { 
    if (str == null) 
     return false; 
    StringBuilder strBuilder = new StringBuilder(str); 
    strBuilder.reverse(); 
    return strBuilder.toString().equals(str); 
} 

당신은 아마이 작업을 수행해야합니다.