Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
62 views
in Technique[技术] by (71.8m points)

java - To-do list and sort

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:

  1. 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.
  2. 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.
  3. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. Standard usage would be:

    List<String> sortedList = new ArrayList<>(stringListCopy)

  2. The issue here is likely in handling newlines. When you call in.nextInt() it is reading an integer from System.in but it is not reading the final newline which is then read by in.nextLine(). Try printing the value you are reading to see what it's doing.

  3. Read the standard tutorial on sorting and lambdas. In summary, Collections.sort takes a Comparator. The Comparator interface has a number of static methods used to create comparators. Comparator.comparing creates a comparator that compares a value generated by a Function. Function is a functional interface so it can take a lambda. The statement you show can be interpreted as: sort the sortedList collection according to the natural ordering of values generated from each entry using the expression arr_dues[stringListCopy.indexOf(s)].

Also note that a much better design would be to encapsulate each event in a single object:

class Event {
    private final String name;
    private final LocalDate dueDate;
}

And have a single list of events:

List<Event> toDoList = new ArrayList<>();

The copying between collections and arrays is really not necessary.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...