Results 1 to 2 of 2
Thread: java help
- 10-06-2009, 06:54 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 1
- Rep Power
- 0
java help
hi everyone, i am facing a problem that is almost impossible for me to solve. the object of program that i have to write is basically with original word and final word(of same length) given, finding out the words that has only one different character from original word. then by repeating the process, reach the final word. what i have no idea about is how do i evaluate/compare two words so that i can find out if the two words are of same length and has only one different character.
the list of valid words for this process will be given along with some basic method to evaluate if a word is in that list, so that part is less painful that comparing the words
what i understand as of now is that in java string is like an array of characters, but what method can i use to create another word with only one character difference? i thought about compareTo method but what i am able to do is returning a simple integer. comparing all words in the valid word list to find out what the next word is as i orginally imagined, i think it would be too much more of work than required to do this job. there has got to be a way but i cant think of any.
can anyone point me in right direction?
- 10-06-2009, 10:54 PM #2
Member
- Join Date
- Aug 2009
- Posts
- 18
- Rep Power
- 0
Sounds as though you're going to need, amongst other things, a method for comparing two words and telling you how many of their corresponding characters are different.
Something like this, maybe?
I've no doubt there are smarter ways of doing this, but this might be a start.Java Code:public class StringComparison { public static void main(String[] args) { StringComparison myStringComparison = new StringComparison(); } public StringComparison() // constructor { System.out.println(compare("flag","flag")); // outputs '0' System.out.println(compare("flag","flog")); // outputs '1' System.out.println(compare("flag","flip")); // outputs '2' System.out.println(compare("flag","face")); // outputs '3' System.out.println(compare("flag","lace")); // outputs '4' System.out.println(compare("flag","laces")); // outputs '-1' } // end constructor private int compare(String firstString, String secondString) { if(firstString.length() != secondString.length()) { return -1; // strings are not the same length } else // compare strings one character at a time, counting the differences { int differences = 0; for(int i =0; i<firstString.length(); i++) { if( !firstString.substring(i,i+1).equals(secondString.substring(i,i+1))) { differences++; } } return differences; } } // end compare method } // end StringComparison class


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks