2012-08-05 5 views
8

나는 자바에서 초보자입니다, 자바로 char에 의해 두 문자열을 비교하려고 얼마나 많은 다른 문자가 다음 코드에 의해 찾을 수 있지만 작동하지 않습니다,자바 문자로 두 문자열 비교 문자로

 min is the min between the 2 strings 

    for(int i=0; i<min-1; i++){ 
      s1 = w1.substring(j,j++); 
      s2 = w2.substring(j,j++); 

      if (! s1.equalsIgnoreCase(s2)){ 
       counter++;  
      } 
     }` 

팁이 있습니까?

+2

카운터는 'i'입니다. 하지만 절대 사용하지 마라. 그것은 루프 안에 있고'j'를 대신 가지고 있습니다. 왜? –

+0

그리고 substring (j, j)가 무엇을 반환 할 것이라고 생각하게합니까? – EJP

+1

코드가 "작동하지 않습니까?" 컴파일 할 때 어떻게됩니까? 컴파일하면 실행됩니까? 실행되면 어떻게됩니까? 그 과정에서 여러분의 기대치와 다른 점은 무엇입니까? 또한 어떤 오류 메시지가 있습니까? –

답변

9

사용이 :

char[] first = w1.toLowerCase().toCharArray(); 
char[] second = w2.toLowerCase().toCharArray(); 

int minLength = Math.min(first.length, second.length); 

for(int i = 0; i < minLength; i++) 
{ 
     if (first[i] != second[i]) 
     { 
      counter++;  
     } 
} 
+0

+1하지만 배열 길이에 대한 테스트를 추가하고 최단 시간까지만 반복합니다. – fvu

+0

@fvu 충분합니다. 그것을 추가했습니다. – Baz

3

수 charAt (인덱스) 메소드를 사용하여 두 문자의 '=='연산자를 사용

c1 = w1.charAt(j); 
c2 = w2.charAt(j); 

if (c1 == c2)){ 
    counter++;  
} 
2
int i =0; 
for(char c : w1.toCharArray())){ 
    if(i < w2.length() && w2.charAt(i++) != c) 
    counter++; 
} 
1

우리의 문제를 해결할 수 substring. 그러나의 처음 코드를 살펴 보자 :

// assuming, min is the minimum length of both strings, 
// then you don't check the char at the last position 
for(int j=0; j < min-1; j++) { 

    // s1, s2 will always be empty strings, because j++ is post-increment: 
    // it will be incremented *after* it has been evaluated 
    s1 = w1.substring(j,j++); 
    s2 = w2.substring(j,j++); 

    if (!s1.equalsIgnoreCase(s2)){ 
    counter++;  
    } 
} 

substring 기반으로하는 솔루션 그렇게 될 수있다 : 수 charAt()와 중첩 루프와 문자열 비교를 필요로하는 자바 교육 튜토리얼

for(int j=0; j < min; j++) { 
    s1 = w1.substring(j,j+1); 
    s2 = w2.substring(j,j+1); 

    if (!s1.equalsIgnoreCase(s2)){ 
    counter++;  
    } 
} 
0

내 노트. ..이 메서드는 소스 문자열에서 일치하지 않는 문자를 반환하도록 쉽게 변경 될 수 있지만 ...--) 당신을 떠날 것입니다 ... ;-)

public class SubString { 

public static boolean findTarget(String target, String source) { 

    int target_len = target.length(); 
    int source_len = source.length(); 

    boolean found = false; 

    for(int i = 0; (i < source_len && !found); ++i) { 

    int j = 0; 

     while(!found) { 

      if(j >= target_len) { 
       break; 
      } 

      /** 
      * Learning Concept: 
      * 
      * String target = "for"; 
      * String source = "Searching for a string within a string the hard way."; 
      * 
      * 1 - target.charAt(j) : 
      * The character at position 0 > The first character in 'Target' > character 'f', index 0. 
      * 
      * 2 - source.charAt(i + j) : 
      * 
      * The source strings' array index is searched to determine if a match is found for the 
      * target strings' character index position. The position for each character in the target string 
      * is then compared to the position of the character in the source string. 
      * 
      * If the condition is true, the target loop continues for the length of the target string. 
      * 
      * If all of the source strings' character array element position matches the target strings' character array element position 
      * Then the condition succeeds .. 
      */ 

      else if(target.charAt(j) != source.charAt(i + j)) { 
       break; 
      } else { 
       ++j; 
       if(j == target_len) { 
        found = true; 
       } 
      } 
     } 

    } 

    return found; 

} 

public static void main (String ... args) { 

String target = "for"; 
String source = "Searching for a string within a string the hard way."; 

System.out.println(findTarget(target, source)); 

} 

} 
관련 문제