Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Using calendar for a train time table

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using calendar for a train time table

    import java.util.*;
    import java.util.Calendar;


    public class TrainTimeTable {


    // Static method to print the date time of a Calendar object, note the get methods
    public static void printCal(Calendar cal) {
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);

    System.out.printf("%4d/%02d/%02d %02d:%02d:%02d\n",
    year, month+1, day, hour, minute, second);
    }

    public static void main(String[] args) {



    //creating class objects
    Station s1 = new Station("Station: Vancouver",null, null, 0);
    Station s2 = new Station("Station: kamloops", null, null, 0);
    Station s3 = new Station("Station: Jasper", null, null, 0);
    Station s4 = new Station("Station: Edmonton", null, null, 0);
    Station s5 = new Station("Station: Saskatoon", null, null, 0);
    Station s6 = new Station("Station: Winnipeg", null, null, 0);
    Station s7 = new Station("Station: Sioux Lookout", null, null, 0);
    Station s8 = new Station("Station: Hornepayne", null, null, 0);
    Station s9 = new Station("Station: Capreol", null, null, 0);
    Station s10 = new Station("Station: Toronto", null, null, 0);
    //creating arraylist
    ArrayList schedule = new ArrayList();
    schedule.add(s1);
    schedule.add(s2);
    schedule.add(s3);
    schedule.add(s4);
    schedule.add(s5);
    schedule.add(s6);
    schedule.add(s7);
    schedule.add(s8);
    schedule.add(s9);
    schedule.add(s10);

    for( Station st : schedule ) {
    System.out.println( st );
    }

    // get an instance of Calendar, default is system data time
    Calendar cal = Calendar.getInstance();
    System.out.print("Current date time is: ");
    printCal(cal);



    // date time arithmetics
    cal.add(Calendar.MINUTE, 30);

    System.out.print("delay of station 10 by 30 min");
    printCal(cal);

    }//end main
    }//end class

    Station Arrival Departure Day
    Vancouver 20:30 1
    Kamloops 06:00 06:35 2
    Jasper 16:00 17:30 2
    Edmonton 23:00 23:59 2
    Saskatoon 08:00 08:25 3
    Winnipeg 20:45 22:30 3
    Sioux Lookout 05:02 05:42 4
    Hornepayne 15:35 16:10 4
    Capreol 00:18 00:48 5
    Toronto 09:30 5

    This is what the time table starts at, I need to pick a station for example Saskatoon then make the train get there 30 minutes later then the original time and since the train will be getting there 30 minutes later it will be getting to all the next stations winnipeg, sioux lookout, hornepayne, capreol and toronto 30 minutes later as well. The only way I know how to do this is by manually changing the times one by one. The question wants me to use a method like this public void delay (String station, int minute) which will add the delay to a station and all the ones after. After that is done i need to display the updated time table. How can I go about doing this??? I have been stuck on this for over a week and am lost. thank you in advance for any help

    I also have this code as well that I was using
    // fix a date time
    cal.set(2018, 01, 25, 00, 00, 00);
    System.out.print("In vancouver: ");
    printCal(cal);

    // fix a date time
    cal.set(2018, 01, 25, 20, 30, 00);
    System.out.print("Leaving Vancouver: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 6, 00, 00);
    System.out.print("Arriving in kamloops: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 6, 35, 00);
    System.out.print("leaving kamloops: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 16, 00, 00);
    System.out.print("arriving in jasper: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 17, 30, 00);
    System.out.print("leaving jasper: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 23, 00, 00);
    System.out.print("arriving in edmonton: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 23, 59, 00);
    System.out.print("leaving edmonton: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 8, 00, 00);
    System.out.print("arriving in saskatoon: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 8, 25, 00);
    System.out.print("leaving saskatoon: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 20, 45, 00);
    System.out.print("arriving in winnipeg: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 22, 30, 00);
    System.out.print("leaving winnipeg: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 5, 02, 00);
    System.out.print("arriving in sioux lookout: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 5, 42, 00);
    System.out.print("leaving sioux lookout: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 15, 35, 00);
    System.out.print("arriving in hornepayne: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 16, 10, 00);
    System.out.print("leaving hornepayne: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 00, 18, 00);
    System.out.print("arriving in capreol: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 00, 48, 00);
    System.out.print("leaving capreol: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 9, 30, 00);
    System.out.print("arriving in toronto: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 00, 00, 00);
    System.out.print("no departures from here: ");
    printCal(cal);

    I want to be able to have the times in my time table instead of null. Sorry i know these are alot of problems but i am very stuck. Thank you again.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Using calendar for a train time table

    instead of null.
    Please copy the full text of the error message and paste it here so we can see where the null values were.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using calendar for a train time table

    import java.util.*;
    import java.util.Calendar;


    public class TrainTimeTable {


    // Static method to print the date time of a Calendar object, note the get methods
    public static void printCal(Calendar cal) {
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);

    System.out.printf("%4d/%02d/%02d %02d:%02d:%02d\n",
    year, month+1, day, hour, minute, second);
    }

    public static void main(String[] args) {



    //creating class objects
    Station s1 = new Station("Station: Vancouver",null, null, 0);
    Station s2 = new Station("Station: kamloops", null, null, 0);
    Station s3 = new Station("Station: Jasper", null, null, 0);
    Station s4 = new Station("Station: Edmonton", null, null, 0);
    Station s5 = new Station("Station: Saskatoon", null, null, 0);
    Station s6 = new Station("Station: Winnipeg", null, null, 0);
    Station s7 = new Station("Station: Sioux Lookout", null, null, 0);
    Station s8 = new Station("Station: Hornepayne", null, null, 0);
    Station s9 = new Station("Station: Capreol", null, null, 0);
    Station s10 = new Station("Station: Toronto", null, null, 0);
    //creating arraylist
    ArrayList schedule = new ArrayList();
    schedule.add(s1);
    schedule.add(s2);
    schedule.add(s3);
    schedule.add(s4);
    schedule.add(s5);
    schedule.add(s6);
    schedule.add(s7);
    schedule.add(s8);
    schedule.add(s9);
    schedule.add(s10);

    for( Station st : schedule ) {
    System.out.println( st );
    }

    // get an instance of Calendar, default is system data time
    Calendar cal = Calendar.getInstance();
    System.out.print("Current date time is: ");
    printCal(cal);



    // date time arithmetics
    cal.add(Calendar.MINUTE, 30);

    System.out.print("delay of station 10 by 30 min");
    printCal(cal);

    }//end main
    }//end class

    Station Arrival Departure Day
    Vancouver 20:30 1
    Kamloops 06:00 06:35 2
    Jasper 16:00 17:30 2
    Edmonton 23:00 23:59 2
    Saskatoon 08:00 08:25 3
    Winnipeg 20:45 22:30 3
    Sioux Lookout 05:02 05:42 4
    Hornepayne 15:35 16:10 4
    Capreol 00:18 00:48 5
    Toronto 09:30 5

    This is what the time table starts at, I need to pick a station for example Saskatoon then make the train get there 30 minutes later then the original time and since the train will be getting there 30 minutes later it will be getting to all the next stations winnipeg, sioux lookout, hornepayne, capreol and toronto 30 minutes later as well. The only way I know how to do this is by manually changing the times one by one. The question wants me to use a method like this public void delay (String station, int minute) which will add the delay to a station and all the ones after. After that is done i need to display the updated time table. How can I go about doing this??? I have been stuck on this for over a week and am lost. thank you in advance for any help

    I also have this code as well that I was using
    // fix a date time
    cal.set(2018, 01, 25, 00, 00, 00);
    System.out.print("In vancouver: ");
    printCal(cal);

    // fix a date time
    cal.set(2018, 01, 25, 20, 30, 00);
    System.out.print("Leaving Vancouver: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 6, 00, 00);
    System.out.print("Arriving in kamloops: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 6, 35, 00);
    System.out.print("leaving kamloops: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 16, 00, 00);
    System.out.print("arriving in jasper: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 17, 30, 00);
    System.out.print("leaving jasper: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 23, 00, 00);
    System.out.print("arriving in edmonton: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 23, 59, 00);
    System.out.print("leaving edmonton: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 8, 00, 00);
    System.out.print("arriving in saskatoon: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 8, 25, 00);
    System.out.print("leaving saskatoon: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 20, 45, 00);
    System.out.print("arriving in winnipeg: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 22, 30, 00);
    System.out.print("leaving winnipeg: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 5, 02, 00);
    System.out.print("arriving in sioux lookout: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 5, 42, 00);
    System.out.print("leaving sioux lookout: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 15, 35, 00);
    System.out.print("arriving in hornepayne: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 16, 10, 00);
    System.out.print("leaving hornepayne: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 00, 18, 00);
    System.out.print("arriving in capreol: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 00, 48, 00);
    System.out.print("leaving capreol: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 9, 30, 00);
    System.out.print("arriving in toronto: ");
    printCal(cal);
    // fix a date time
    cal.set(2018, 01, 25, 00, 00, 00);
    System.out.print("no departures from here: ");
    printCal(cal);

    I want to be able to have the times in my time table instead of null. Sorry i know these are alot of problems but i am very stuck. Thank you again.

Similar Threads

  1. School Time Table
    By smarties in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 14th, 2014, 09:12 AM
  2. Java Calendar printing wrong Time Zone?
    By jean28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 2nd, 2013, 04:59 AM
  3. Replies: 4
    Last Post: January 7th, 2012, 01:59 PM
  4. Replies: 1
    Last Post: January 6th, 2012, 07:02 AM