biblioman
biblioman

Reputation: 3

Reading data from a text file in Android returns nothing?

It's my first question, but I'll try to ask it well. Anyway, I'm trying to read data from a text file in Android, but for some reason it keeps returning nothing - namely, IndexOutOfBounds exception. All relevant code:

public Question getNextQuestion(int num) {
    Log.d(TAG, array.toString());
    return array.get(num+1);
}
public ArrayList<Question> getData(String str) throws NumberFormatException, IOException {
    Log.d(TAG, "STARTING getData");
    Log.d(TAG, str);
    ArrayList<Question> a = new ArrayList<Question>();
    String[] strline = str.split("//");
    for (int j = 0; j < strline.length; j++) {
        if (strline[j] == null) {
            strline[j] = "TESTING";
            Log.d(TAG, strline[j]);
        }
    }

    int num = 0;
    int x = 0;
    String y = strline[x];
    while (x == 0 || y != null) {
        num++;
        String[] data = y.split("//");
        Log.d(TAG, y);
        if (data.length >= 7) {
            a.add(new Question(Integer.parseInt(data[0]), data[1], data[2], data[3], data[4], data[5], Integer.parseInt(data[6])));
            Log.d(TAG, a.get(a.size()).getText());
        }
        else if (data.length >= 3) {
            a.add(new Question(Integer.parseInt(data[0]), data[1], data[2]));
            Log.d(TAG, a.get(a.size()).getText());
        }
        x++;
        if (x < strline.length)
            y = strline[x];
        else
            break;
    }
    for (int i=0; i<a.size(); i++){
        Log.d(TAG, a.get(i).getText());
    }
    Log.d(TAG, "ENDING getdata");
    return a;
}
public ArrayList<Question> openFile() throws IOException {
    Log.d(TAG, "STARTING openFile()");
    //FileReader fr = new FileReader("data/data/scibowl.prog/questionsb.txt");
    FileInputStream fis;
    String storedString = "";
    try {
        fis = openFileInput("questionsb.txt");
        DataInputStream dataIO = new DataInputStream(fis);
        String strLine = null;

        while ((strLine = dataIO.readLine()) != null) {
            storedString = storedString + strLine;
        }

        dataIO.close();
        fis.close();
    }
    catch  (Exception e) {  
    }

    array = getData(storedString);
    Log.d(TAG, "DID open file, ending openFile()");
    return array;
}

This will print in LogCat:

    onCreate done
STARTING openFile()
STARTING getData
ENDING getdata
DID open file, ending openFile()
right before Toast.makeText
[]
Caused by: java.lang.IndexOutOfBoundsException: Invalid location 1, size is 0
at java.util.ArrayList.get(ArrayList.java:341)
at scibowl.prog.Round.getNextQuestion(Round.java:55)
at scibowl.prog.RoundView.<init>(RoundView.java:26)
at scibowl.prog.Round.onCreate(Round.java:35)

I've tried practically everything from StringBuffers to FileInputStreams to BufferedReaders, and read the related Android documentation, though feel free to tell me I'm not reading it hard enough. Does anybody know why I keep getting arrays with no elements?

Text file for reference, albeit slightly edited to conform with the blockquote rules:

1//On the standard electromagnetic spectrum, which frequency is between gamma rays and ultraviolet rays?//Radio waves//Microwaves//X-rays//Infrared waves//1

2//Which waves on the electromagnetic spectrum roughly encompasses frequencies of 10^9 Hz to 10^12 Hz?//Visible Light//Gamma Rays//Ultraviolet Rays//Microwaves//3

3//Approximately what percentage of the US’s power comes from coal?//30%//40%//50%//60%//0

4//If a 100-kilogram person starts walking from rest and accelerates to 4.0 m/s over 2 seconds at constant acceleration, how much work does he do?//600 J//800 J//1200 J//2000 J//1

Upvotes: 0

Views: 385

Answers (1)

Reed
Reed

Reputation: 14974

Your logcat suggests it is crashing on line 55 in the method getNextQuestion, which I believe is:

return array.get(num+1);

Also, your logcat suggests that you're trying to access a value in the array that doesn't exist. Meaning you're trying to retrieve the item at index 1 (the second item, I believe), but the size of your array is 0, so there are no items that you can get.

So chances are that in getData you're failing to actually return an ArrayList like you intend. Or the ArrayList you return is empty.

Upvotes: 1

Related Questions