1
\$\begingroup\$

I have, in Java, an arraylist with these values (many lines, this is just an extract):

20/03/2013 23:31:46 6870 6810 6800 6720 6860 6670 6700 6650 6750 6830 34864 34272 20/03/2013 23:31:46 6910 6780 6800 6720 6860 6680 6620 6690 6760 6790 35072 34496

where the first two values are strings that contain data and are stored in a single element.

What I want to do is to compare the string data elements and delete, for example, the second one and all the elements referred to that line.

For now, I've used a for cycle that every 13 elements compares the string (in order to compare only data strings).

Can I implement this with other better solutions?

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Downsampler {
    public static void main(String[] args) throws Exception{
        //The input file
        Scanner s = new Scanner(new File("prova.txt"));

        //Saving each element of the input file in an arraylist    
        ArrayList<String> list = new ArrayList<String>();
        while (s.hasNext()){
            list.add(s.next());
        }
        s.close();

        //Arraylist to save modified values
        ArrayList<String> ds = new ArrayList<String>();

        int i;
        for(i=0; i<=list.size()-13; i=i+14){
            //combining the first to values to obtain data    
            String str = list.get(i)+" "+list.get(i+1);
            ds.add(str);
            //add all the other values to arraylist ds
            int j;
            for(j=2; j<14; j++){
                ds.add(list.get(i+j));
            }

            //comparing data values
            int k;    
            for(k=0; k<=ds.size()-12; k=k+13){
                ds.get(k); //first data string element    

                //Comparing with other strings and delete
                //TODO    
            }
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Please, for future maintainer/international safety, use an ISO-formatted locale-independent 'local'-timezone-ignorant timestamp. In the US, there's about something like 7 or 8 different times that those stamps could be, given timezones. If the day-of-month wasn't greater than 12, you have the potential to switch month/day-of-month, depending on reader. \$\endgroup\$ – Clockwork-Muse Jul 9 '13 at 18:48
2
\$\begingroup\$

To answer your question.. it depends on how you want to handle the data. Take a step back and put your self in the users shoes. Lets say the user has those numbers saved as bank account transactions.. He would want those numbers saved somewhere, but not necessarily visible. If that was the case you wouldn't want "delete" that data. You could, however, parse the data into say a table. You could give each cell in that table a class that has a boolean value called visible, and a int value. Then you could set a row or column to not visible as you wish. That would give you the ability to then just loop through the rows and columns and output the data as needed.

\$\endgroup\$
3
  • \$\begingroup\$ In my case it's important that in case of duplicates, i keep only one value because i have duplicates every seconds and one line of values it's enough. But still i try to find a good solution \$\endgroup\$ – alessandrob Jul 9 '13 at 13:24
  • \$\begingroup\$ @Markviduka if it is in a table you can filter it as needed. There is probably a way to do this easily, I just don't know how to do it in java. \$\endgroup\$ – Robert Snyder Jul 9 '13 at 14:57
  • \$\begingroup\$ I solved, i used a simple if statement and equals to strings. Thanks a lot \$\endgroup\$ – alessandrob Jul 9 '13 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.