0

PLEASE,

I know this question has been asked many times, but I still can't get my code to work.

I have a text file in my assets folder called words.txt. There is one word per line in each line of that text file with no blank lines. I want to put every word in an array.

First I tried using scanner, but after reading many stackoverflow threads I found out I needed to use AssetManager.

Here's what I tried:

AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("words.txt");

It gives me the error message: default constructor cannot handle exception type IOException thrown by implicit super constructor... before I even do anything else. I don't know what this means.

Also, looking at the api, it seems like InputStream can only read bytes. How can I read words from the text file?

The threads are overwhelming because it seems like each answer proposes a different method, using InputStreams, BufferedStreams, FileInputStreams, File, Res folders, Asset folders, and lots of other language I am unfamiliar with. I am just learning to develop android apps and have limited java experience.

gallardoelise
  • 342
  • 3
  • 17
  • The `AssetManager` and the `Scanner` are both necessary. The `Scanner` does what you point out at the end: `byte[]` to `String`. – salezica Dec 26 '13 at 07:42
  • possible duplicate of [While constructing the default constructor can not handle exception : type Exception thrown by implicit super constructor](http://stackoverflow.com/questions/6772709/while-constructing-the-default-constructor-can-not-handle-exception-type-excep) – Sachin Dec 26 '13 at 07:42

4 Answers4

4

You can do as shown below:

    try {
        BufferedReader bReader = new BufferedReader(new InputStreamReader(getAssets().open("file.txt")));
        ArrayList<String> values = new ArrayList<String>();
        String line = bReader.readLine();
        while (line != null) {
            values.add(line);
            line = bReader.readLine();
        }
        bReader.close();
        for (String v : values)
            Log.i("Array is ", v);
    } catch (IOException e) {
        e.printStackTrace();
    }
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
0

Use this:

Resources rs = getResources();
InputStream inputStream = rs.openRawResource(R.raw.sample);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
while ((line = r.readLine()) != null) {
    array.append(line);
}
Gabe
  • 1,239
  • 1
  • 13
  • 20
0

You can use xml for loading the words like below:

res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>
</resources>

In class file:

Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

Source

fida1989
  • 3,234
  • 1
  • 27
  • 30
0

put you array inside res/values/strings.xml as

 <string-array name="spinnerNames">
    <item>Trust</item>
    <item>Grade</item>
    <item>Location</item>
    <item>Contract Type</item>
    <item>Speciality</item>

</string-array>

and load your array as:

 String[] spinnerNames = getResources().getStringArray(R.array.spinnerNames);
Nabin Khatiwada
  • 478
  • 5
  • 15