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 12 of 12

Thread: Write a program that reads the timetable from a file and prints out to the screen?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Write a program that reads the timetable from a file and prints out to the screen?

    **Initial Format saved as a text document:**

    04
    05 10 25 40 55
    06 05 15 25 35 45 55
    07 00 05 10 15 20 25 30 35 40 45 50 55
    08 00 05 10 15 20 25 30 35 40 45 50 55
    09 00 05 10 15 20 25 30 35 40 45 50 55
    10 00 05 10 15 20 25 30 35 40 45 50 55
    11 00 05 10 15 20 25 30 35 40 45 50 55
    12 00 05 10 15 20 25 30 35 40 45 50 55
    13 00 05 10 15 20 25 30 35 40 45 50 55
    14 00 05 10 15 20 25 30 35 40 45 50 55
    15 00 05 10 15 20 25 30 35 40 45 50 55
    16 00 05 10 15 20 25 30 35 40 45 50 55
    17 00 05 10 15 20 25 30 35 40 45 50 55
    18 00 05 10 15 20 25 30 35 40 45 50 55
    19 00 05 10 15 20 25 30 35 40 45 50 55
    20 00 05 10 15 20 25 30 35 40 45 50 55
    21 05 15 25 40 55
    22 10 25 40 55
    23 10 25 40 55 55
    00 10 25 40 55
    01 17 47
    02 17 47
    03 17 47

    **Desired result should look like this:**

    05:10 05:25 05:40 05:55
    06:05 06:15 06:25 06:35 06:45 06:55
    07:00 07:05 07:10 07:15 07:20 07:25 07:30 07:35 07:40 07:45 07:50 07:55
    08:00 08:05 08:10 08:15 08:20 08:25 08:30 08:35 08:40 08:45 08:50 08:55
    09:00 09:05 09:10 09:15 09:20 09:25 09:30 09:35 09:40 09:45 09:50 09:55
    10:00 10:05 10:10 10:15 10:20 10:25 10:30 10:35 10:40 10:45 10:50 10:55
    11:00 11:05 11:10 11:15 11:20 11:25 11:30 11:35 11:40 11:45 11:50 11:55
    12:00 12:05 12:10 12:15 12:20 12:25 12:30 12:35 12:40 12:45 12:50 12:55
    13:00 13:05 13:10 13:15 13:20 13:25 13:30 13:35 13:40 13:45 13:50 13:55
    14:00 14:05 14:10 14:15 14:20 14:25 14:30 14:35 14:40 14:45 14:50 14:55
    15:00 15:05 15:10 15:15 15:20 15:25 15:30 15:35 15:40 15:45 15:50 15:55
    16:00 16:05 16:10 16:15 16:20 16:25 16:30 16:35 16:40 16:45 16:50 16:55
    17:00 17:05 17:10 17:15 17:20 17:25 17:30 17:35 17:40 17:45 17:50 17:55
    18:00 18:05 18:10 18:15 18:20 18:25 18:30 18:35 18:40 18:45 18:50 18:55
    19:00 19:05 19:10 19:15 19:20 19:25 19:30 19:35 19:40 19:45 19:50 19:55
    20:00 20:05 20:10 20:15 20:20 20:25 20:30 20:35 20:40 20:45 20:50 20:55
    21:05 21:15 21:25 21:40 21:55
    22:10 22:25 22:40 22:55
    23:10 23:25 23:40 23:55 23:55
    00:10 00:25 00:40 00:55
    01:17 01:47
    02:17 02:47

    I have stored the file to an arrayList but would like to manipulate things to get the desired format? Any suggestions/help is highly appreciated. This is what i have written so far.

     
     package TestPaket;
     
    import java.io.File;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Scanner;
     
    public class BustimeTable {
     
    	private static ArrayList<Time> timesArray = new ArrayList<Time>();
     
     
    	public static void main(String[] args) {
     
    		Scanner scanner = null;
     
    		try {
     
    		File file = new File("C:/Users/Tommy/workspace/Prov/SLtid.txt");
    	    scanner = new Scanner(file);
    		while(scanner.hasNextLine()){
    			String[] tokens = scanner.nextLine().split("\\s+");
     
    				for (int i= 0; i< tokens.length; i++)
    				{
     
    					Time t = new Time(tokens[i], tokens[i].substring(01));
     
    				// new time ( hour, minute )
     
     
    			System.out.println(t);	
    				}
     
    		}} catch (Exception e) { 
    			e.printStackTrace();
    		}}}

    I have another class called the time class that basically sends the hour and minutes to the main class:

     
    public class Time {
     
    		int hr;
    		int minute;
     
    		Time(String t, String m){
     
    		hr = Integer.parseInt(t);
    		minute = Integer.parseInt(m);
     
    	}
     
     
     
    	public String toString(){
     
    		return String.format("%02d:%02d", hr, minute);
     
     
    }}
    Last edited by tednfs; March 10th, 2013 at 05:51 PM. Reason: Formatting code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Write a program that reads the timetable from a file and prints out to the screen?

    Does the data have the hours in the first column and the minutes in the rest of the columns?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    NOTE: }s should be on a line by themselves.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Write a program that reads the timetable from a file and prints out to the screen?

    Yes, the data has the hours in the first column and the minutes in the rest of the columns!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Write a program that reads the timetable from a file and prints out to the screen?

    Can you explain what your problems are? What does the program's output look like?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Write a program that reads the timetable from a file and prints out to the screen?

    My problem is that the output is wrong. The result being outputed can be viewed below.

    I am trying to read the times from a text file, format it and present it as shown in the second file above. However, i am having problems in getting the hour(05,06,07,08,09,10 etc) from each line and then adding the remaining minutes with THIS hour time:

    So from 05 10 25 40 55 in a given file i would get 05:10 05:25 05:40 05:55! Kindly have a look at the two files presented above.

    As of now, my result looks like this:
    04:04
    05:05
    10:00
    25:05
    40:00
    55:05
    06:06
    05:05
    15:05
    25:05
    35:05
    45:05
    55:05
    07:07
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    08:08
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    09:09
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    10:00
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00

    45:05
    50:00
    55:05
    11:01
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    12:02
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    13:03
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    14:04
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    15:05
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    16:06
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    17:07
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    18:08
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    19:09
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    20:00
    00:00
    05:05
    10:00
    15:05
    20:00
    25:05
    30:00
    35:05
    40:00
    45:05
    50:00
    55:05
    21:01
    05:05
    15:05
    25:05
    40:00
    55:05
    22:02
    10:00
    25:05
    40:00
    55:05
    23:03
    10:00
    25:05
    40:00
    55:05
    55:05
    00:00
    10:00
    25:05
    40:00
    55:05
    01:01
    17:07
    47:07
    02:02
    17:07
    47:07
    03:03
    17:07
    47:07

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Write a program that reads the timetable from a file and prints out to the screen?

    To put more items on one line do NOT use the println() method for every item.
    Use the print() method to put items on the same line and use println() one time at the end of a line to move to the next line.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Write a program that reads the timetable from a file and prints out to the screen?

    I made those changes and ended up with: The logic still doesnt hold up:S

    public class SLtimeTable {
     
    	private static ArrayList<Tid> timesArray = new ArrayList<Tid>();
     
     
    	public static void main(String[] args) {
     
    		Scanner scanner = null;
     
    		try {
     
    		File file = new File("C:/Users/Ralph/workspace/Prov/SLtid.txt");
    	    scanner = new Scanner(file);
    		while(scanner.hasNextLine()){
    			String[] tokens = scanner.nextLine().split("\\s+");
     
    				for (int i= 0; i< tokens.length; i++)
    				{
     
    					Tid t = new Tid(tokens[i], tokens[i].substring(01));
     
    				// new time ( hour, minute )
     
     
    			System.out.print(t);	
    				}
     
    				System.out.println();
    		}} catch (Exception e) {
    			e.printStackTrace();
    		}}}

    04:04
    05:0510:0025:0540:0055:05
    06:0605:0515:0525:0535:0545:0555:05
    07:0700:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    08:0800:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    09:0900:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    10:0000:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    11:0100:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    12:0200:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    13:0300:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    14:0400:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    15:0500:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    16:0600:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    17:0700:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    18:0800:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    19:0900:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    20:0000:0005:0510:0015:0520:0025:0530:0035:0540:00 45:0550:0055:05
    21:0105:0515:0525:0540:0055:05
    22:0210:0025:0540:0055:05
    23:0310:0025:0540:0055:0555:05
    00:0010:0025:0540:0055:05
    01:0117:0747:07
    02:0217:0747:07
    03:0317:0747:07

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Write a program that reads the timetable from a file and prints out to the screen?

    The logic still doesnt hold up:S
    Can you explain what the problem is?
    I see that You need to add a space as a separator between the data that is printed:
    print(data + " ")
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Write a program that reads the timetable from a file and prints out to the screen?

    I have tried to work around it but cant put my fingers around it. I realise that the hour prints itself twice before printing the remaining minutes. Can you point me in the right direction or suggest how i could tackle this problem.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Write a program that reads the timetable from a file and prints out to the screen?

    When you print a time, concatenate a space after it:
    System.out.print(t + " ");

    How does the code get the hours value so it can be used with each printed time?
    The hours value is in the first column, the minutes in the following columns.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Write a program that reads the timetable from a file and prints out to the screen?

    --- Update ---

    [/COLOR]Thank you. It works as it should. Regarding your inquires, the hour value is gotten from token[0] which is the first index of a new line.

    According to the requirement: the hour 04 should not be printed out in the new format which is the case in my situation. Why does this hold true in my case? Curious to find out. Thank you so much!

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Write a program that reads the timetable from a file and prints out to the screen?

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    tednfs (March 10th, 2013)

Similar Threads

  1. Replies: 10
    Last Post: November 8th, 2012, 06:29 AM
  2. Replies: 2
    Last Post: November 7th, 2012, 10:45 PM
  3. Write a Java program that reads file
    By Mehwish-S in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 28th, 2012, 03:00 PM
  4. Replies: 2
    Last Post: November 30th, 2011, 07:35 PM
  5. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM

Tags for this Thread