0

because i am only reading a very simple csv, where only strings are comma separated and should be converted to a String[].

I thought this was so easy a external jar would be a bit to much and i could handle this very easy. But what happens is that the first item get added until the memory is full and crash!

public List readWinkels(Activity a){ List winkelList = new ArrayList();

        try{
            InputStream winkelcsv =  a.getResources().getAssets().open("winkels.csv");

            BufferedReader br = new BufferedReader(new InputStreamReader(winkelcsv, "UTF-8"));
            String s  = br.readLine();
            while (s != null){
                winkelList.add(s);
                System.out.println(s.toString());
            }

            br.close();
            for(int i =0;i<winkelList.size();i++) {
                System.out.println(winkelList.get(i));
                }
        }catch(IOException ioe){
            ioe.printStackTrace();
        }

        return winkelList;

here is my code.... i dont get why it doesnt work, can anyone help? A readline reads the line and then the reading points jumps to the next line (i think) so why is the first line added a zillion times?

1 Answer 1

2

Here's the standard idiom for using a while loop to iterate over lines of a file, applied to your code:

String s;
while ((s = br.readLine()) != null){
    winkelList.add(s);
    System.out.println(s.toString());
}

You need to call readLine() at every iteration through the loop. The original code is nothing but an infinite loop, since s is only read once. Assuming s is not null, the loop condition is never false, so the list grows without bound until all available memory is used.

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.