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.

2

4 Answers 4

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

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);
}

Comments

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

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.