2017-02-03 1 views
-3
public class Tester{ 
    public static void main (String args[]){ 

    String word="sandwich"; 
    char newWord[]=word.toCharArray(); 

    System.out.println(word.equals(transform(newWord))); 
    if (word.equals(transform(newWord))){ 
     System.out.println("same");} 
     else{ 
     System.out.println("not"); 
     } 

    } 
    public static String transform(char [] newWord){ 
    for(int i=0; i<newWord.length;i++) 
    { 
     System.out.print(String.valueOf(newWord[i])); 
     //System.out.print(""+newWord[i]); 

    } 
    return ""; 
    } 
    } 

결과 >> 샌드위치 거짓 샌드위치하지변환 연결된 문자 (문자)

리터럴 문자열로 문자의 연결된 '문자열을'변환하는 방법이 있나요?

+0

'System.out.println (word.equals (newWord)); '사용 – cpx

+0

감사합니다,하지만 이것은 작동하지 않는 예제입니다. 변경된 문자열은 문자열 리터럴과 동일하지 않으므로 .equals()는 해당 문자열을 동일한 것으로 인식하지 않습니다. – Richard

+0

당신이하고 싶은 것을 보여줄 수 있습니까? [ideone] (http://ideone.com/)을 시도하십시오. – cpx

답변

0

문자열을 ==과 비교해서는 안됩니다 (정확히 무엇을하고 있는지 모르는 경우).

대신 .equals()을 사용하십시오.

+0

새로 형성된 '문자열'문자는 문자열 리터럴과 동일하지 않으므로 .equals는 false입니다. 어떻게 새로 생성 된 문자를 문자열 리터럴로 변환 할 수 있습니까? – Richard