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);
// 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);
}
//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;
}
Sebastian Zaba
Greenhorn
Joined: Nov 30, 2010
Posts: 5
posted
0
OK I think i got it. But now:
Its not able to find the hard coded dictionary file how do i enter it so android can find it after installing the apk?
Scanner s = new Scanner(new File("dictionary.txt"));
I am having trouble using getResources() if it is the correct one to use.