Can someone explain how I would go about checking that the new valid word entered is only one character different from previous word???

Recommended Answers

oldString.length() != newString.length();

not so hard

Jump to Post

All 4 Replies

oldString.length() != newString.length();

not so hard

Just start writing a simple method eg
public static boolean isValidChange(String word1, String word2) { ...
You have 3 cases to test for - change, add, remove a letter. Code and test those 1 at a time. Start with 1 letter changed, that's probably the easiest.

How would you be able to check to see if only one letter was changed?

I am able to check if a letter was removed/added using .length(), but I can't seem to figure out a method to check if it has been changed..... Thank you in advance

Joe

set the value of the second string,
set a difCounter to 0
iterate over the original one by charAt, and compare the char's. if the char's are different, add 1 to your counter.
then replace your second string by the concatenation of the two substrings (part of that string before the different char, part of that string after that different char)
and continue searching for differences, starting from the same position

if your counter turns out to be > 1, you've got more than one character changed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.