0

So I am working on a project that will take command line arguments (ex: 11:45:12 11:48:13) and output in the form "Elapsed time in seconds is: 181"

Though I am having difficulty changing my code from asking the user to "start" the timer by hitting any key, to getting the direct time from the command line argument. Any assistance is greatly appreciated! Below are my two classes Clock, and TestClock. testClock is the one that will be run with the command line arguments.

    import java.sql.Time;

 public class Clock {
   public Time startTime;
   public Time endTime;

   Clock() {
   } public void start(String startTime){
       startTime.startTime(convertToSeconds(startTime));


   } public void stop(String endTime){
       endTime.setTime(convertToSeconds(endTime));
   }

import java.util.Scanner;


public class TestClock {
public static void main(String args[]){
    Clock cl = new Clock();
    cl.start(args[0]);
    cl.stop(args[1]);
    System.out.println("Time elapsed is " + cl.getElapsedTime() + " seconds");
}

}

Still receiving error unable to convert to seconds is not able to be found. Same with get elapsed time.

3 Answers3

0

Remove System.in and use args[0] and args[1]. args in main() method signature is an array of command line arguments.

Everv0id
  • 1,862
  • 3
  • 25
  • 47
0

You'll need a way to set stopTime and startTime. I recommend creating another constructor that takes two strings. Alternatively, you could add new stop and start functions that let you set them.

Once you have that then, as EvervOid mentions, use the corresponding position in the args parameter to grab the command line arguments and pass them to your new stop and start functions. (You don't need to delete the ones you already have, as Java is smart and can tell the difference between two functions with the same name and different parameters).

The final main function might look something like:

public class TestClock {
    public static void main(String args[]){
        Clock cl = new Clock(args[0], args[1]);
        System.out.println("Time elapsed is " + cl.getElapsedTime() + " seconds");
    }
}

EDIT: per comment question

You could either have a constructor that accepts the start and end times like so:

   Clock(String startString, String endString) {
       startTime = new Time(convertToMilliseconds(startString));
       endTime = new Time(convertToMilliseconds(endString));
   }

You'll need to write the conversion functions per the other post I mentioned.

The other way would be to write the following to functions:

   public void start(String startTime){
       startTime.setTime(convertToMilliseconds(startTime));
       System.out.println("Start time is " + startTime);
   }

   public void stop(String endTime){
       endTime.setTime(convertToMilliseconds(endTime));
       System.out.println("End time is " + endTime);
   }

If you do this then the main function becomes:

public class TestClock {
    public static void main(String args[]){
        Clock cl = new Clock();
        cl.start(args[0]);
        cl.end(args[1]);
        System.out.println("Time elapsed is " + cl.getElapsedTime() + " seconds");
    }
}
Alejandro
  • 1,168
  • 8
  • 11
  • If you need help converting your string into milliseconds you might consider this [post](http://stackoverflow.com/questions/8826270/how-to-convert-hhmmss-sss-to-milliseconds). – Alejandro Mar 02 '15 at 00:42
  • What exactly do you mean new stop and start functions to set them, what would that look like. (sorry, having a brain fart currently. I understand the riddance of Scanner and the "press any key to start/stop time" – David Cline Mar 02 '15 at 00:56
  • Now I get the error, cannot find symbol convetToMilliSeconds(string) – David Cline Mar 02 '15 at 01:36
  • public class Clock { public Time startTime; public Time endTime; Clock() { } public void start(String startTime){ startTime.startTime(convertToSeconds(startTime)); } public void stop(String endTime){ endTime.setTime(convertToSeconds(endTime)); } } – David Cline Mar 02 '15 at 02:13
  • It was an example of a function you could write. You need to still convert the string to milliseconds somehow. How to convert from a string to a time is well covered in other questions like the one I linked to in my first comment. – Alejandro Mar 02 '15 at 02:16
  • I got it, I appreciate your assistance, you were a great help for me today! – David Cline Mar 02 '15 at 02:35
0
import java.sql.Time;
public class Clock {

   //Create two instance of Time class with default values
   //for hour,minute and seconds
   private Time startTime=new Time(0, 0, 0);
   private Time stopTime=new Time(0, 0, 0);



   //set start time
   public void start(Time startTime){
       this.startTime=startTime;
   }


   //set end time
   public void stop(Time stopTime){
       this.stopTime=stopTime;
   }



   /**The method getElapsedTime calculates the time difference
   * between two times and returns seconds as return value.
   * */
   public int getElapsedTime(){
       int totalSeconds=0;
       int hours=stopTime.getHours()-startTime.getHours();
       if(hours>0) {          
           int minues=stopTime.getMinutes()-startTime.getMinutes();
           if(minues>0) {
               minues=stopTime.getMinutes()-startTime.getMinutes();
           }
           else
               minues=startTime.getMinutes()-stopTime.getHours();

           int seconds=stopTime.getSeconds()-startTime.getSeconds();
           if(minues>0) {
               seconds=stopTime.getSeconds()-startTime.getSeconds();
           }
           else
               seconds=startTime.getSeconds()-stopTime.getSeconds();


           totalSeconds=hours*60*60+minues*60+seconds;
       }
       else {

           int minutes=stopTime.getMinutes()-startTime.getMinutes();
           if(minutes>0) {
               minutes=stopTime.getMinutes()-startTime.getMinutes();
           }
           else
               minutes=startTime.getMinutes()-stopTime.getMinutes();

           int seconds=stopTime.getSeconds()-startTime.getSeconds();
           if(seconds>0) {
               seconds=stopTime.getSeconds()-startTime.getSeconds();
           }
           else
               seconds=startTime.getSeconds()-stopTime.getSeconds();


           totalSeconds=hours*60*60+minutes*60+seconds;
       }
       //return seconds              
       return totalSeconds;
   }//end of the getElapsedTime method


}//end class

import java.sql.Time;
public class TestClock {
   public static void main(String[] args) {


       //Create an instance of Clock class object
       Clock clock=new Clock();      
       //Get first argument
       String startTime=args[0];

       //split the argument and get hour,minute and second
       String [] time=startTime.split(":");

       //split the argument and get hour,minute and second
       int hour=Integer.parseInt(time[0]);
       int minutes=Integer.parseInt(time[1]);
       int seconds=Integer.parseInt(time[2]);


       //call start method with an instance Time class
       clock.start(new Time(hour, minutes, seconds));

       //Get first argument
       String endTime=args[1];
       String [] stopTime=endTime.split(":");

       //split the argument and get hour,minute and second
       hour=Integer.parseInt(stopTime[0]);
       minutes=Integer.parseInt(stopTime[1]);
       seconds=Integer.parseInt(stopTime[2]);
       //call start method with an instance Time class
       clock.stop(new Time(hour, minutes, seconds));

       System.out.println("Elapsed time in seconds :"+clock.getElapsedTime());

   }//end main

}//end class