1

I would like to ask for help with this task: I have CSV for example like this:

column1$column2$column3
123$xyz$321
456$zyx$654

And I would like to parse it by Java to Arrays / Array lists by columns / headers -> for example

ArrayList column1 = [123,456]
ArrayList column2 = [xyz,zyx]
ArrayList column3 = [321,654]

Thanks everyone.

4
  • split, loop and add to list... Commented Nov 15, 2015 at 21:03
  • but how to loop through columns? Commented Nov 15, 2015 at 21:03
  • line.split("$") will give you and array, index 0 ---> list1 (column1) , index 1 --> list 2 column2 ecc. Commented Nov 15, 2015 at 21:06
  • 1
    Welcome to Stack Overflow. Please read stackoverflow.com/help/how-to-ask to help improve your post. Commented Nov 15, 2015 at 21:09

2 Answers 2

2

This is how I would have done this..., note the metod to put the columns in another List for less code and to be more dynamic.

public static void main(String[] args) {
    ArrayList<ArrayList<String>> columns = new ArrayList<ArrayList<String>>();
    BufferedReader br = null;
    try {
        String sCurrentLine;
        br = new BufferedReader(new FileReader("testing.cvs"));

        while ((sCurrentLine = br.readLine()) != null) {
            String[] fields = sCurrentLine.split("\\$");
            for (int i = 0; i < fields.length; i++) {
                if (columns.size()<=i){
                    columns.add(new ArrayList<String>());
                }
                columns.get(i).add(fields[i]);
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it works well. But could You please be so gentle and extend Your solution by automatic-counter of columns? So I did not have to manually enter the value of columns?
I am sorry, maybe I wrote my question wrong. Now I am not sure, if this is what I needed. I need to access to every column by String index (the header of column). Should I use HashMaps instead of ArrayLists?
Thats surely one method to do it... but I guess you need to open a new question, if you have still problem... you should use a HashMap that contains ArrayList...
OK, I will open another question, Thank you.
0

I thin this can help you to fix the problem just tery it.

public static void main(String[] args) {
    Scanner s = null;
    try {
        s = new Scanner(new File("C:\\temp\\file.txt"));
        ArrayList lis1 = new ArrayList();
        ArrayList lis2 = new ArrayList();
        ArrayList lis3 = new ArrayList();

        while (s.hasNext()) {

            String d = s.nextLine();
            lis1.add(d.split("\\$")[0]);
            lis2.add(d.split("\\$")[1]);
            lis3.add(d.split("\\$")[2]);

        }

        for (Object l : lis1) {
            System.out.print(l+" ");
        }
        System.out.print("\n ");
        for (Object l : lis2) {
            System.out.print(l+" ");
        }
        System.out.print("\n ");
        for (Object l : lis3) {
            System.out.print(l+" ");
        }

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

you can use the result when you wont

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.