sorting java task by user-entered dates -
i'm creating tasks list in java , want able sort data user-entered due dates. below have far. case 3 of switch statement want sort variable duedate in case 2: how access this? use collection.sort date?
import java.io.*; import java.util.scanner; public class main { public static string filename = "/users/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(); break; case 0: break; default: system.out.println("invalid input"); } } } static int menu(){ int choice; scanner sc = new scanner(system.in); system.out.println("\n task list menu \n"); system.out.println("0: exit menu"); system.out.println("1: show tasks in list"); system.out.println("2: add task list"); system.out.println("3: sort tasks due date"); system.out.println(); system.out.println("enter choice: "); choice = sc.nextint(); return choice; } static void showtasklist(){ system.out.println("\ntask list\n"); try { scanner infile = new scanner(new filereader(filename)); string line; int number = 1; while(infile.hasnextline()){ line = infile.nextline(); system.out.println(number + ". " + line); ++number; } system.out.println(); infile.close(); } catch (filenotfoundexception ioe) { system.out.println(ioe); } } static void addtask(){ system.out.println("\nadd task\n"); try { scanner input = new scanner(system.in); printwriter outfile = new printwriter(new filewriter(filename, true)); system.out.println("enter task: "); string addedtask = input.nextline(); system.out.println("set due date task(yyyy-mm-dd): "); string duedate = input.nextline(); outfile.println(addedtask + " " + duedate); outfile.close(); } catch (ioexception ioe) { system.out.println(ioe); } } static void sortlist(){ system.out.println("\nsorted list\n"); } }
yes, can use collections.sort().
however, you'll need implement comparable or comparator add in own logic sort it.
try searching comparable , comparator , should see how it.
Comments
Post a Comment