Я продолжаю получать эту ошибку Исключение в потоке "main" java.lang.ArrayIndexOutOfBoundsException: 0
Исключение в потоке "main" java.lang.ArrayIndexOutOfBoundsException: 0 at homework.TestClock.main(TestClock.java:10)
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
Все методы get и время начала и окончания не работают
Я приму всю помощь, которую смогу получить.
1 ответ
Я хочу знать, как вы передаете переменную String[] args? Когда вы установите это?
//how you pass value for this String[] args?
public static void main(String[] args) {
//if String[] args is null, there must be error
//Get first argument
String startTime=args[0];