I'm working on my school project and one of my idea was to create a to-do list by asking
Step 1. Enter your event
Step 2. user enters
Step 3. Enter your due date (decimal form)
Step 4. user enters
Step 5. Ask if user wants to enter more events (1 for yes, 0 for no)
Step 6. while user didn't enter 0, repeat the process 1-5
Then I want my code to sort my event according to my due date. I found something on StackOverflow that works for my program but I have the following problems:
- When I run my program the system shows:
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
I've no idea why and hope this to be solved.
- When I run my program, Step 2 is skipped after the first event is entered
Which means only 1 event and due date is entered, then you can only enter due dates.
- I've no idea how this line of code works
Collections.sort(sortedList, Comparator.comparing(s -> arr_dues[stringListCopy.indexOf(s)]));
Which I should be able to understand if I'm submitting this. If you can, can you please explain a little about comparators or drop a link for a video that I would be able to watch to understand this concept.
Thank you very very very much. Here is my code.
static void to_do() {
int more = -1;
String event;
double due = 0.0;
Scanner in = new Scanner(System.in);
ArrayList<String> events = new ArrayList <String>();
ArrayList<Double> dues = new ArrayList <Double>();
System.out.println("To-Do List Generator:");
while (more != 0) {
System.out.println("Please enter your event");
event = in.nextLine();
events.add(event);
System.out.println("Please enter the due date in decimal form");
due = in.nextDouble();
dues.add(due);
System.out.println("Do you have more events to add? Yes enter 1, No enter 0");
more = in.nextInt();
}
System.out.println("Entry Successful. Here is your to do list");
String[] arr_events = new String[events.size()];
double[] arr_dues = new double[dues.size()];
for (int i=0; i<events.size();i++){
arr_events[i] = events.get(i);
}
for (int i=0; i<dues.size();i++){
arr_dues[i] = dues.get(i);
}
final List<String> stringListCopy = Arrays.asList(arr_events);
ArrayList<String> sortedList = new ArrayList(stringListCopy);
Collections.sort(sortedList, Comparator.comparing(s -> arr_dues[stringListCopy.indexOf(s)]));
String[] returnarr = new String[stringListCopy.size()];
Collections.sort(dues);
for (int i = 0; i<stringListCopy.size(); i++) {
returnarr[i] = sortedList.get(i);
}
for (int i = 0; i<arr_dues.length; i++) {
System.out.print(returnarr[i] + ", Due: ");
System.out.println(dues.get(i));
}
}
public static void main(String[] args) {
to_do();
}
question from:
https://stackoverflow.com/questions/65877058/to-do-list-and-sort 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…