0

I am trying to read from a text file but and those Strings separate into different attributes but I don't know how to follow after the first split.

Here is my code: What should be the offset of the getType() string?

try {
        InputStream is = context.getAssets().open("Autoeval");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
         //Skips lines
        for (int i = 0; i< questionNumber; i++) {
            reader.readLine();
        }

        question = reader.readLine();

    } catch (IOException e) {
        e.printStackTrace();
    }
}



public String getId() {

    return question.substring(0, question.indexOf(";"));
}
public String getType() {
    return question.substring(question.indexOf(";"));
}

1 Answer 1

1

It's ugly, but why don't you create 2 global private variables:

private String _id;
private String _type;

Then, after you read in the question, you can do this:

{
    //stuff

    question = reader.readLine();

    _id = question.substring(0, question.indexOf(";"));
    _type = question.substring(_id.length); // assuming no other ";" delimiters

}

public String getId() {
    return _id;
}

public String getType() {
    return _type;
}

All this being said, there are much better ways to do this.

Sign up to request clarification or add additional context in comments.

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.