posted 11 years ago
First I wanted to thank all the devoted programmers to this site it has been a large help to me. Now lets post something :) I spend 2 days on this now and its driving me crazy I am new to Android development and only somewhat comfortable with JAVA itself. Here is my problem I am writing a quick Android app to take a String permute it and check it against a dictionary but the onClick will only invoke and display the permute and not any of my other functions I tested the functions on a separate Java only tester class and the methods work just fine. here is the relevant Code:
public class HackaWordActivity extends Activity implements OnClickListener {
TextView enterLetters;
TextView possibleWords;
Button button1;
String inputString;
char[] chars;
ListView listView;
static ArrayAdapter<String> adapter; //to fill matches and display
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
enterLetters = (TextView) this.findViewById(R.id.enterLetters);
listView = (ListView) findViewById(R.id.mylist);
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(this);
}
//get user input
public String getString() {
inputString = (enterLetters.getText().toString());
return inputString;
}
@Override
public void onClick(View v) {
// adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1,Permutation.permute("", getString())); //this works and displays the results in the desired listView.
try {
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1,Permutation.compared(getString()));
//this does not work
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listView.setAdapter(adapter);
}
}
The Permutation Class:
//Permutation is a separate class with contains the methods to be implemented in HackaWordActivity
public class Permutation {
static ArrayList<String> wordArray = new ArrayList<String>();
public static String word;
public static String dictionaryCheck; //temporary holding place for dictionary words to compare
public static ArrayList<String> matches = new ArrayList<String>();
//recursive method that permutes chars in a given String. It takes a blank string, and a String
//It returns an ArrayList with the permuted chars as Strings
public static ArrayList<String> permute(String beginningString, String endingString) {
if (endingString.length() <= 1) {
word = (beginningString + endingString);
wordArray.add(word);
}
else
for (int i = 0; i < endingString.length(); i++) {
try {
String newString = endingString.substring(0, i) + endingString.substring(i + 1);
permute(beginningString + endingString.charAt(i), newString);
} catch (StringIndexOutOfBoundsException exception) {
exception.printStackTrace();
}
}
return wordArray;
}
//Method to check if permuted Strings match words in a hard coded dictionary
public static ArrayList<String> checkDictionary (ArrayList<String> permutedArrayList) throws FileNotFoundException{
Scanner s = new Scanner(new File("dictionary1.txt")); //Creates scanner and reads file that must be in the Project folder
while (s.hasNext()){ //scan the document and set each word as String dictionaryCheck
dictionaryCheck = s.next();
//check the provided arrayList to the dictionary Word saved in dictionaryCheck
for (int word = 0; word<permutedArrayList.size(); word++){
if (permutedArrayList.get(word).equals(dictionaryCheck)){
matches.add(permutedArrayList.get(word));
}
}
}
return matches;
}
//method that simplifies use my combining permuting and matching
public static ArrayList<String> compared (String input)throws FileNotFoundException{
checkDictionary(permute("",input));
return matches;
}