2012-08-05 80 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)); 

} 

}