Search if String differs by one character

I am trying to determine if the entered word differs by one character in a text file. I have code that works, but unfortunately only for words with two characters or less, which is clearly not very useful, and the code itself looks a bit messy. Here is what I still have:

if(random.length() == word.length()){
  for(int i = 0; i < random.length(); i++){
    if( (word.charAt(i) == random.charAt(i))){
      str += word+"\n"; 
      count++;
    }
  }
 }  

C randomwill be the word entered by the user, and word- the word to search in a text file.

If I change my second statement ifto something along the lines

if( (word.charAt(i) == random.charAt(i)) && (word.charAt(i -1) == random.charAt(i-1)))

and if I change int ito be = 1, I seem to get more of what I'm looking for, but then my code only searches if the first two letters match, and if the last two also what it should do.

+4
2

, ? .

static boolean equals(String word1, String word2, int mistakesAllowed) {
    if(word1.equals(word2)) // if word1 equals word2, we can always return true
        return true;

    if(word1.length() == word2.length()) { // if word1 is as long as word 2
        for(int i = 0; i < word1.length(); i++) { // go from first to last character index the words
            if(word1.charAt(i) != word2.charAt(i)) { // if this character from word 1 does not equal the character from word 2
                mistakesAllowed--; // reduce one mistake allowed
                if(mistakesAllowed < 0) { // and if you have more mistakes than allowed
                    return false; // return false
                }
            }
        }
    }

    return true;
}
+6

, , , .

:

int count = 0;     if(random.length() == word.length()) {
for(int i = 0; i < random.length(); i++)
{
    if( (word.charAt(i) != random.charAt(i) ))
    {
        if(count == 0)
        {
            System.out.println("Found first difference!");
        }
        if(count != 0)
        {
            System.out.println("Strings are more than one letter different!");
        }
        count++;
    }
} }

, , , . : String1 = "abc"; String2 = "zzzabcdef";

6 6 . , : def, cde, abc, zab, zza, zzz, zzb, zzc, zzd, zze, zzf, zaf, zae, zad, zac, zab, zza, zzf, zze,...,..., , 9 6, .

, , , . for, 0 + 1 . i- , - 1 , , .. , .

, , .

, , , , . , , . :

String testAgainst = "lookingForWordsOneLetterDifferentThanThisString";
int words = 0;

Scanner scan = new Scanner(fileName);

while(scan.hasNext())
{
    String word = scan.next();
	
    if( isOneDifferent(word, testAgainst) )
    {
        words++;
    }

    System.out.println("Number of words one letter different: " + words);
}

public boolean isOneDifferent(String word, String testAgainst)
{
    if(word.length() != testAgainst.length())
    {
        return false;
    }

    int diffs = 0;

    for(int i = 0; i < word.length(); i++)
    {
        if(word.charAt(i) != testAgainst.charAt(i))
        {
            diffs++;
        }
		
        if(diffs > 1)
        {
            return false;
        }
    }

    if(diffs == 1)
    {
        return true;
    }
    else
    {
        return false;
    }

}
Hide result
0

All Articles