2012-08-05 99 views
8

我是初學者在Java中,我想比較兩個字符串中的字符和char 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

計數器是'我'但你永遠不會使用它在循環內部,並有一些'j'。爲什麼? – 2012-08-05 22:00:45

+0

什麼讓你覺得substring(j,j)會返回任何東西? – EJP 2012-08-05 22:02:43

+1

你的代碼怎麼樣「不起作用?」編譯它時會發生什麼?如果它編譯,它運行?如果它運行,會發生什麼?一路上,發生了什麼不同於你的期望?另外,如果有的話,你會得到什麼錯誤信息? – 2012-08-05 22:02:58

回答

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 2012-08-05 21:58:06

+0

@fvu夠公平的。添加它。 – Baz 2012-08-05 21:59:16

3

使用的charAt(index)方法,並使用 '==' 操作兩個字符:

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一個解決辦法是這樣的:

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

我從需要的charAt()和嵌套循環字符串比較的java培訓教程筆記。 ..該方法可以很容易地更改爲從源字符串中返回不匹配的字符...但是我會將其留給您... ;-)

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

} 

}