0

This is my method for getting the .txt file inside a .java class

public static void find() throws IOException
{
    @SuppressWarnings("resource")
    Scanner sc = new Scanner(new FileReader("AllWords.txt"));


    while( sc.hasNextLine() )
    {
        String word = sc.nextLine();
        words.add(word);
        String sortedWord = sortString(word); // this is a key

        ArrayList<String> anagrams = map.get( sortedWord );  //this is a value

        if( anagrams == null ) anagrams = new ArrayList<String>();

        anagrams.add(word);
        map.put(sortedWord, anagrams);
    }

}

-When I call find() inside an Activity class it wont read the .txt file, but I have tested this code in another program and it opens the file, but since I moved to Android - development I haven't been able to read this file.

-I have also tried to create this method inside an Activity class and android environment does not like "SortString".

-Could the problem be the location of my .txt file?

Here is the location: enter image description here

Bauer
  • 159
  • 4
  • 13

2 Answers2

0
  1. Why would you use a TextFile ? You can use the String.xml that are default for Android development. See: http://developer.android.com/guide/topics/resources/string-resource.html. This can reduce the code line.

  2. A Resource file needs to be in the RES directory not in the source package; See: http://developer.android.com/guide/topics/resources/providing-resources.html

I hope this may help you.

Best Regards,

Bruno
  • 136
  • 8
  • the file is a hundred thousand words, the algorithm takes a word and finds all Anagrams then sorts them in a HashMap with the Word(Key), and a ArrayList(Value) of the words anagrams. – Bauer Jun 11 '14 at 18:06
0

You should place the Text file in the "assets" folder and use the AssetManager built-in class.

You can create a new Scanner using:

new Scanner(new File(getAssets().open("AllWords.txt")));

If the new File() doesn't work, use an InputStreamReader :

new InputStreamReader(getAssets().open("filename.txt"));
krodmannix
  • 845
  • 10
  • 30