1

So I currently store a bunch of string titles together into a file on the user's phone and then read it later (when they relaunch the app). I'm trying to read back the string and split it on the delimiter I set for it but for some reason it splits it and then doubles the string...

So for example if I stored these strings

Ricky(har)Bobby(har)is(har)cool(har)

(har) is the delimiter I use to store them. (for example)

For some reason, when I use the split function on "har"

It gives me an array of strings, but doubles how many I stored... So the array of strings would have two Ricky's two Bobby's two is's and two cool's. I'm at a loss for what's going on to be honest. Been staring at this for hours... anyone have any idea?

line = BR.readLine();
            String[] each = line.split("<TAG>");
            for (int i = 0; i < each.length; i++) {
                listOfCourses.add((each[i]));
                //Toast.makeText(context, each[i], Toast.LENGTH_SHORT).show();
            }

Here's the function that stores the data to the user's phone

//adds data into the classes file on user's phone
    public void addClassesIntoFile(Context context, ArrayList<String> classList) {
        try {
            FileOutputStream fos = context.openFileOutput(CLASSLIST_FILENAME,
                    Context.MODE_PRIVATE | Context.MODE_APPEND);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            for (int i = 0; i < classList.size(); i++) {
                osw.write(classList.get(i) + "<TAG>");
            }
            osw.flush();
            osw.close();
        } catch (FileNotFoundException e) {
            // catch errors opening file
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
7
  • maybe you are writing them twice :/ Commented Feb 4, 2013 at 18:27
  • Try adding a Log.v in line 2 as: Log.v("Test", line); and see what has been stored Commented Feb 4, 2013 at 18:27
  • I see what it's reading in with the toast messages. I've also checked the writing to the file. It writes correctly :( Commented Feb 4, 2013 at 18:28
  • please post more code. this block looks like it's working correctly, creates an array from the split() then writes that array to an ArrayList, and finally a second ArrayList based off the first (which I gotta wonder why you need to do it twice, but that really shouldn't matter) Commented Feb 4, 2013 at 18:31
  • yeah, I got rid of the 2nd arrayList. Kinda redundant. Commented Feb 4, 2013 at 18:36

2 Answers 2

2

I don't believe your split function is the problem and instead think it's with some other part of your code.

  1. Is the String before you conduct the split correct? Or do you see duplications in it?
  2. I imagine the problem is with your addClassIntoFile method. Since you are appending into the File, be sure you aren't re-adding values that are already there. Unless you delete that file at some point, it'll persist the next time the app is launched.
  3. Finally make sure you aren't calling addClassIntoFile more then you meant too. IE, accidentally invoking it more then once on a given object.

If I'm incorrect on all this, then please post an example of what the String looks like before a split so we can be sure the given regex is correct.

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

1 Comment

Thanks...it was something reallllly stupid completely unrelated to any of this, but still, lol. Just happy to have it fixed.
0

Replace:

String[] each = line.split("<TAG>")` 

with this:

String[] each= line.split("[(har)]");

if the wrong still, tell me please

10 Comments

why -1 please? I thought this is the wrong with his code, and the rest will work good if he replace his split way with mine
His code does not contain either tokens or roma - what exactly is he to replace? Your answer needs to be clarified. If he has to guess how to apply your answer, it's not an answer, it's a hint.
@Francesco: I don't think he meant he literally used (har) as the delimiter - his question says "(for example)". Also, why would this work?
@SoftwareMonkey it will work because when you use [] it will be reqular expression , I tried it on eclipse and it works
its ok, appreciate it! I'll keep you updated if I figure it out :->
|

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.