Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2188
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T17:42:04+00:00 2022-05-31T17:42:04+00:00

arrays – Why does Java is considering the older date bigger than the last date?

  • 0

[ad_1]

I have a Java application that saves the path and last modification date of all files with the specified extension in the specified folder, recursivelly, in a CSV file.

I have another application that receives a keyword from the user and looks for a file which file name contains that word, opens the last modified one.

It was working as expected, but one of the users found a bug, where, with a specific file (until now) the it opens the one with the older modification date instead of the last one.

Here is the CSV file filtered with the item bugged: CSV file

This is the code where the application filters the csv data to find only the last modified file which the name contains the keyword:

public class OpenListener implements ActionListener {
    private TwoComboBox frame;
    private List<String[]> drawList = new ArrayList<String[]>();

    public OpenListener(TwoComboBox frame, List<String[]> drawList) {
        this.frame = frame;
        this.drawList = drawList;
    }

    public void actionPerformed(ActionEvent e) {
        /**
         * Open the file or directory of file of the last modified file
         in the folder to look with the specified file name
        **/
        // Get user preferences
        final String fileType =
            String.valueOf(frame.getCombo1().getCombo().getSelectedItem());
        final String openMethod =
            String.valueOf(frame.getCombo2().getCombo().getSelectedItem());

        // Filter
        drawList = filterDrawList(drawList, fileType.toLowerCase());

        // On click
        String drawPath = replaceUnmappedChars(drawList.get(0)[2]);
        try {
            if (openMethod.equals(SearchListener.OPEN_FOLDER_OPTION)) {
                frame.dispose();
                String dirPath =
                    (new File(drawPath)).getParentFile().getAbsolutePath();
                Desktop.getDesktop().open(new File(dirPath));
            } else {
                frame.dispose();
                Desktop.getDesktop().open(new File(drawPath));
            }
        } catch (IOException ex) {
            showErrorDialog("'" + drawPath + "' não encontrado");
        }
    }

    private static List<String[]> filterDrawList(
        List<String[]> drawListToFilter, String fileTypeToFilter
    )
    {
        /**
         * Returns filtered draw list
        **/
        drawListToFilter = DTManager.filterTable(drawListToFilter,
                                                 fileTypeToFilter,
                                                 1);
        return DTManager.getLastObjects(
             drawListToFilter,
             0,
             3,
             DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
         );
    }
}

This is the code that filters the data:

public class DTManager{

    public static List<String[]> getLastObjects(List<String[]> table,
                                                int toFilterIndex,
                                                int dateIndex,
                                                DateTimeFormatter dtFormatter)
    {
        /**
         * Returns a list with only unique items filtered by the instances with
         the last date
        **/
        List<String[]> list = new ArrayList<String[]>();
        List<String[]> currData = new ArrayList<String[]>();
        LocalDateTime date;
        String dateStr;
        List<LocalDateTime> dates = new ArrayList<LocalDateTime>();
        int lastDateIndex;
        for (int i = 0; i < table.size(); i++) {
            currData = filterTable(table,
                                   table.get(i)[toFilterIndex],
                                   toFilterIndex);
            dates = new ArrayList<LocalDateTime>();
            for (int j = 0; j < currData.size(); j++) {
                date = LocalDateTime.parse(currData.get(j)[dateIndex],
                                           dtFormatter);
                dates.add(date);
            }
            lastDateIndex = dates.indexOf(Collections.max(dates));
            list.add(currData.get(lastDateIndex));
            table = inverseFilterTable(table,
                                       table.get(i)[toFilterIndex],
                                       toFilterIndex);
        }
        return list;
    }

    public static List<String[]> filterTable(List<String[]> table,
                                             String valueToLook,
                                             int toFilterIndex)
    {
        /**
         * Returns a list with only items where the passed value appears in the
         specified column
        **/
        List<String[]> list = new ArrayList<String[]>();
        for (int i = 0; i < table.size(); i++) {
            if (table.get(i)[toFilterIndex].contains(valueToLook)) {
                list.add(table.get(i));
            }
        }
        return list;
    }

    public static List<String[]> inverseFilterTable(List<String[]> table,
                                                    String valueNotToLook,
                                                    int toFilterIndex)
    {
        /**
         * Returns a list with only items where the passed value does not
         appears in the specified column
        **/
        List<String[]> list = new ArrayList<String[]>();
        for (int i = 0; i < table.size(); i++) {
            if (!table.get(i)[toFilterIndex].contains(valueNotToLook)) {
                list.add(table.get(i));
            }
        }
        return list;
    }
}

And this is the method that transform the CSV data into a List<String[]>:

public List<String[]> getDB() throws FileNotFoundException, IOException{
        /**
         * Returns a table with the data of the CSV file
        **/
        List<String[]> data = new ArrayList<String[]>();
        CSVParser parser = new CSVParserBuilder().withSeparator(sep).build();
        CSVReader reader =
            new CSVReaderBuilder(new FileReader(csvPath))
                .withCSVParser(parser).build();
        String[] nextLine;
        while ((nextLine = reader.readNext()) != null) {
            data.add(nextLine);
        }
        return data;
    }

I’ve already removed the miliseconds from the modification datetimes, thinking this could be the problem but it was not the case. Unfortunally I’m out of ideas.

If you want adventure yourselfs:

Repository where is the app that writes the csv file data:
https://github.com/xlurio/drawings-admin/

  • The csv must be created previously, it only writes it.
  • Set the CSV path and the folder you want to get information on: “src/main/java/com/calegario/drawingsadmin/Settings.java”.

Repository where is the app that looks for the files:
https://github.com/xlurio/drawings-finder/

  • Set the CSV path on: “src/main/java/com/calegario/drawingsfinder/Settings.java”.

Dependencies:
https://github.com/xlurio/file-database-gen/blob/main/target/filedbgen-1.0-SNAPSHOT.jar
https://github.com/xlurio/csv-database/blob/main/target/csvdb-1.0-SNAPSHOT.jar
https://github.com/xlurio/datatable-manager/blob/main/target/dtmanager-1.0-SNAPSHOT.jar
https://github.com/xlurio/default-windows/blob/main/target/defaultwins-1.0-SNAPSHOT.jar

To run the applications, you need to manually install the dependencies using maven, and then use the command “mvn clean compile assembly:single” to compile the apps.

Obs: I’m sorry if the code it’s not readable enough, I didn’t had the time to refactor it yet, but if it’s too hard I’ll do it soon as possible.

[ad_2]

  • 0 0 Answers
  • 9 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4794 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1063 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1009 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.