dimanche 1 mars 2015

Changing java code from user input to command line argument



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 {
private Time startTime;
private Time endTime;

Clock() {
startTime = new Time(System.currentTimeMillis());
endTime = new Time(System.currentTimeMillis());
}

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

public void stop(){
endTime.setTime(System.currentTimeMillis());
System.out.println("end time is " + endTime);
}

public int getElapsedTime(){
return ((endTime.getHours() - startTime.getHours()) * 60
+ (endTime.getMinutes() - startTime.getMinutes()) * 60
+ (endTime.getSeconds() - startTime.getSeconds()));

}

}





import java.util.Scanner;


public class TestClock {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
Clock cl = new Clock();
System.out.println("Enter some value to set start time to current time");
in.next();
cl.start();
System.out.println("Enter some value to set end time to current time");
in.next();
cl.stop();
System.out.println("Time elapsed is " + cl.getElapsedTime() + " seconds");
}
}



Aucun commentaire:

Enregistrer un commentaire