#put time slots in order of start time

5 messages · Page 1 of 1 (latest)

steady rivet
#

Hi I am working on a practice question and I am trying to read in the number of bookings for a day and their time allocations. I would then like to take the start times of each booking and order the time slots from earliest booking to latest. So far I am to read in the number of bookings and the time slots but I am unsure of how to reorder the time slots. I have attached the code I have done thus far as well as the expected input and output
‘’’

import java.util.Scanner;

public class Bookings {
public static void main(String []args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of bookings:");
int numberOfBookings = keyboard.nextInt();
System.out.println("Enter the bookings, one per line:");
for (int i = 0; i <= numberOfBookings; i++){
String timeslots = keyboard.nextLine();
}
keyboard.close();
}
}
/*
Input:
Enter the number of bookings:
5
Enter the bookings, one per line:
9:00 am - 12:00 pm
1:00 pm - 6:00 pm
10:30 am - 2:30 pm
8:30 am - 9:00 am
8:00 am - 10:00 pm

Output:
Bookings in order of Start Time:
8:00 am - 10:00 pm
8:30 am - 9:00 am
9:00 am - 12:00 pm
10:30 am - 2:30 pm
1:00 pm - 6:00 pm
*/
‘’’

pseudo quiverBOT
#

This post has been reserved for your question.

Hey @steady rivet! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

proud zodiac
#

first off, you may want to make a class for the timeslot rather than using a string, so you can just parse the string once instead of having to do it multiple times

proud zodiac
#

anyways, you first need a structure to hold all the bookings/timeslots
if you make a class as mentioned before, you can have that class implement Comparable and then just use a SortedSet like TreeSet to have automatic sorting
alternatively, you can create a Comparator and use that for the SortedSet instead of having a compareTo method using Comparable