0

I am trying to create a method that will take in user entered tasks and due dates and sort them by due date. I have the code written for the task and it compiles, but when I try to sort it seems to delete all the contents. Am I writing over the original file? How could I correct this to add it to the same file without deleting?

public class DueDate implements Comparable<DueDate>{

    public String addedTask = "";
    public String dueDate = "";


    public DueDate(String addedTask, String dueDate){

        this.addedTask = addedTask;
        this.dueDate = dueDate;
    }

    public String toString(){
        return addedTask+"\t"+dueDate+"\t";
    }

    @Override
    public int compareTo(DueDate o) {
        return this.dueDate.compareTo(o.dueDate);
    }
}

public class Main {

    public static String fileName = "/Users/eri/Desktop/tasklistjava/src/javatask.txt";

    public static void main(String[] args) throws IOException {

        int menuItem = -1;
        while(menuItem != 0){
            menuItem = menu();
            switch (menuItem){
                case 1:
                    showTaskList();
                    break;
                case 2:
                    addTask();
                    break;
                case 3:
                    sortList();
                case 4:
                    deleteTasks();
                    break;
                case 0:
                    break;
                default:
                    System.out.println("Invalid Input");

            }
        }
    }

static void sortList() throws IOException {
        System.out.println("\nSorted List\n");
        try {
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
            ArrayList<DueDate> tasks = new ArrayList<DueDate>();
            String line = "";
            while((line = br.readLine()) != null) {
                String[] values = line.split("-");

                if(values.length == 2) {
                    String addedTask = values[0];
                    String dueDate = values[1];

                    DueDate d = new DueDate(addedTask, dueDate);

                    tasks.add(d);
                }
            }

            Collections.sort(tasks);

            for(int i = 0; i < tasks.size(); i++){
                DueDate date = tasks.get(i);
                String lineText = date.toString();
                bw.write(lineText);
                bw.newLine();
            }

            br.close();
            bw.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
2
  • 1
    would it be easier to sort if the dates were Dates instead of a string? Commented Feb 7, 2016 at 16:00
  • You can't sort existing file data without overwriting the file. Commented Feb 8, 2016 at 12:21

1 Answer 1

1

Usually when you want to sort records in a file, you'd first take the contents out and store it in an arraylist and sort it whilst it's in the arraylist. Afterwards you delete the old file and write the sorted data into a new file.

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.