2016-02-29 52 views
-2

線程 「main」 java.lang.ArrayIndexOutOfBoundsException異常:0 在homework.TestClock.main(TestClock.java:10)我一直在線程收到此錯誤異常 「主要」 java.lang.ArrayIndexOutOfBoundsException: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 

所有的get方法和開始時間和結束時間不工作

我接受了所有我能得到的幫助。

+0

主()需要開始和結束時間,以在被傳遞以字符串格式當代碼被激發,例如:「10點25分32秒」,「12點05分○○秒」 – DevilsHnd

+0

@DevilsHnd我如何在命令行中做到這一點? –

+0

我想指出你在這裏:https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html – DevilsHnd

回答

0

我想知道,你如何傳遞變量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]; 
+0

它應該打印java TestClock 11:45:12 11:48:13 經過的時間在秒是:181 –

相關問題