0

I have a text file with the data as shown below as an example..

2;30;1;801;2;1951;195102;1111;
2;30;1;801;3;1621;162101;1111;
2;30;1;802;1;1807;180702;1111;
2;30;1;802;1;1807;180703;1111;
2;30;1;802;1;1807;180704;1111;
2;30;1;802;2;1101;110101;1111;
2;30;1;802;2;1139;113902;9999;
2;30;1;802;2;1948;194801;1111;
2;30;1;802;2;1948;194802;1111;
2;30;1;802;3;2477;247701;1111;
2;37;2;803;1;2006;200601;0000;
2;37;2;803;1;2006;200602;0000;
2;37;2;803;2;2005;200501 ;0000;

In my program I have to put a filter and then display, which groups the Rows i.e. Row[3] based on Row[7]. My list should appear like this after filtering for example only for 802

802;1;1807;180702;1111;
802;1;1807;180703;1111;
802;1;1807;180704;1111;
802;2;1101;110101;1111;
802;2;1139;113902;9999;
802;2;1948;194801;1111;
802;2;1948;194802;1111;
802;3;2477;247701;1111;

similarly for 803

803;1;2006;200601;0000;
803;1;2006;200602;0000;
803;2;2005;200501 ;0000;

The problem is the value of Row[3] is not constant and keeps varying throughout the file in a group for example a bunch of values are 802, the next bunch 804, 806 etc and so on. which should be grouped like as shown above. I already have a FileReader for reading the file, but can anyone tell me a logic on how to go about applying this filter?

code for reading file and placing values in a list

public ArrayList<Asset> getData() {

        ArrayList<Asset> list = new ArrayList<Asset>(); 
        try {
        BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            Asset sd = null;
            while ((line = reader.readLine()) != null ) {
                 String[] RowData = line.split(";");
                 if (RowData.length >= 19) {
                     sd = new Asset();
                    sd.setProductID(RowData[3]);
                    sd.setProductName(RowData[7]);
                    sd.setStatus(RowData[13]);
                    list.add(sd);
                    }
                }   
                return list;
                }

I am confused as to what logic to add in order to filter RowData[3] in the while loop while reading from the reader

user3747512
  • 203
  • 1
  • 6
  • 15
  • Please include the Java code you have tried and describe what your problem with it is. –  Jun 27 '14 at 10:18
  • take a look on this question about reading CSV files in Java: http://stackoverflow.com/questions/10960213/how-to-read-comma-separated-values-from-text-file-in-java – logoff Jun 27 '14 at 10:20
  • If the value of `Row[3]` 'cannot be seen until the runtime, it is dynamic and keeps changing' I don't understand what you want to do. Do you want to filter the rows for *one* value of `Row[3]`? For which value? How is this value determined? Or do you want to *group* the rows for *all* values of `Row[3]` that occur in the file? –  Jun 27 '14 at 10:37
  • In other words, how do I group Row[3] which are of identical values other than hardcoding the value and checking for it.. – user3747512 Jun 30 '14 at 09:18

1 Answers1

0

First you filter out the rows with row[3] == 803 (or so) and then you sort based on row[7] like

Collections.sort(filteredCsvRowsCollection,new Comparator<Integer[]>(){
   @Override
   public int compare(final Integer[] lhs,final Integer[] rhs) {
         if (lhs[7] > rhs[7]) { return 1; } 
         else if (lhs[7] < rhs[7]) { return -1; }
         else { return 0; }
     }
}

Which is adopted from How to use Comparator in Java to sort

Community
  • 1
  • 1
claj
  • 5,172
  • 2
  • 27
  • 30
  • But the value for Row[3] cannot be seen until the runtime, it is dynamic and keeps changing. Is there anyway not to hardcode this value of Row[3] – user3747512 Jun 27 '14 at 10:35
  • Sorry, I would not solve this problem in java, but Clojure with clojure.data.csv, the solution would be something like (sort-by #(nth % 7) (filter #(= (nth % 3) 803) (csv/read-csv in-file) as at https://github.com/clojure/data.csv – claj Jul 04 '14 at 17:26