Results 1 to 2 of 2
  1. #1
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Problem with displaying the time

    WHAT IT DOES: The application reads in the first line of standard input. For each line of input, if the user enters exit, the application terminates; otherwise, the application interprets the line as a name of a text file. The application creates or recreates this file and writes to it two lines of output, the name of the file and the current date and time. The application then closes the file, reopens it for reading, and writes its content to standard output. The application writes to standard output the name of the file as well as the date and time.

    MY PROBLEM: Everything works perfectly but when it’s time to print out the output (filename and time), the seconds in time is the same for all. In this example the seconds is always 33 seconds but I waited at least 3 seconds before I entered each filename.

    AN EXAMPLE OUTPUT:
    Enter a filename of type 'exit' to quit: essay.txt
    Enter a filename of type 'exit' to quit: math.txt
    Enter a filename of type 'exit' to quit: english.txt
    Enter a filename of type 'exit' to quit: CSCI.txt
    Enter a filename of type 'exit' to quit: exit

    [essay.txt]
    1: essay.txt
    2: Sat Sep 13 14:23:33 HST 2014

    [math.txt]
    1: math.txt
    2: Sat Sep 13 14:23:33 HST 2014

    [english.txt]
    1: english.txt
    2: Sat Sep 13 14:23:33 HST 2014

    [CSCI.txt]
    1: CSCI.txt
    2: Sat Sep 13 14:23:33 HST 2014

    THE SOURCE CODE:

    Java Code:
    package projectassignment1;
    
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.util.Scanner;
    import java.io.*;
    
    /**
     * Author: <<Conrado Sanchez>> Date: Task:
     */
    public class ProjectAssignment1 {
    
        public static void main(String[] args) throws IOException {
            Calendar cal = new GregorianCalendar();
            Scanner keys = new Scanner(System.in);      // create scanner object 
            String input = "H";                         // hold input 
            String fileName = "ProjectAssignment1.txt"; // file name 
            String time;
            int day, month, year;
            int second, minute, hour;
    
            // create file 
            PrintWriter outputFile = new PrintWriter(fileName);
    
            // get filename or exit to quit 
            while (input.compareToIgnoreCase("exit") != 0) {
                System.out.print("Enter a filename of type 'exit' to quit: ");
                input = keys.nextLine();
    
                // if input does not equal to exit it prints the input and time
                // to the file. Otherwise nothing happens. 
                if (input.compareToIgnoreCase("exit") != 0) {
                    outputFile.println(input);          // write file name to the file 
                    outputFile.println(cal.getTime());  // wrtie date and time to the file
                } else if (input.compareToIgnoreCase("exit") == 0) {
                    // nothing
                }
            }
            // flush and close 
            outputFile.flush();
            outputFile.close();
    
            // consume line
            System.out.println();
    
            // display the data 
            File file = new File(fileName);
            Scanner inputFile = new Scanner(file);
            while (inputFile.hasNext()) {
    
                // display filename as well as time
                String line = inputFile.nextLine();
                if (line.compareToIgnoreCase("exit") != 0) {
                    System.out.println("[" + line + "]");
                    System.out.println("1: " + line);
                    time = inputFile.nextLine();
                    System.out.println("2: " + time);
    
                    // space
                    System.out.println();
                }
            }
            // close file 
            inputFile.close();
    
        } // end of main()
    } // end of ProjectAssignment1 class

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Problem with displaying the time

    You're writing cal.getTime() to each file for the same 'cal' object; a Calendar doesn't have a running time (or clock); it is a snapshot with a fixed time, so all files end up with a same time printed to it. Either create a new Calendar for each file in that while loop, but computers are so fast nowadays that all times end up identical (within the second) ... or you have to manipulate the Calendar object to see a time difference per file.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. JPanel is not Displaying all the time
    By SunDontShine in forum New To Java
    Replies: 13
    Last Post: 08-17-2014, 02:48 PM
  2. Displaying Date and Time in GUI
    By no_slip_condition in forum New To Java
    Replies: 10
    Last Post: 06-09-2013, 04:00 PM
  3. Replies: 8
    Last Post: 02-26-2013, 05:41 PM
  4. Question for Displaying time
    By siva_vs_siva in forum New To Java
    Replies: 5
    Last Post: 10-29-2010, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •