0

I would like to read an array in from a file that is in the format of

{{"text text text","text text","also text"},{"text text text","text text","also text"},{"text text text","text text","also text"}}

from a file and don't know the best way to do it. How can I efficiently split the line in order to add them to an ArrayList in a for loop?

1 Answer 1

1

string.split(",\\{") will return an array of entries in the line separated by commas. You could use this and some nested fors to fill your ArrayLists. Although if you're saving to Android, you should be using SharedPreferences and saving sets of items.

Example of shared prefs:

    // Adds an item to the shared preferences list
    public void addItemToList(String item){
        SharedPreferences itemList = getSharedPreferences(PREFS_NAME, 0);
        Set<String> items =  itemList.getStringSet(filter, new HashSet<String>());
        Set<String> tItems =  new HashSet<String>();
        tItems.addAll(items); 

        tItems.add(item);

        SharedPreferences.Editor editor = itemList.edit();
        editor.putStringSet(filter, tItems);

        editor.commit();
    }

Example of nested for:

String[] lineArray = line.split(",\\{");
for(int i =0; i < lineArray.length ; i++){
    String[] currentArray = lineArray[i].split(",");
    for(int j = 0; j < currentArray.length ; j++){
    ....
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.