String split with parseInt in Java -


we asked build constructor stopwatch takes string in format of "##:##:###" , updates minutes, seconds , milliseconds (private instance variables) accordingly. example, "1:21:300" indicates 1 minute 21 seconds 300 milliseconds.

so trying use string.split() paired parseint update values. however, program not compile. constructor has correct syntax according eclipse, there wrong doing. have never used split nor parseint, using these 100% wrong. thank you.

    public stopwatch(string starttime){     string [] timearray = starttime.split(":");      if(timearray.length == 2){         this.minutes = integer.parseint(timearray[0]);         this.seconds = integer.parseint(timearray[1]);         this.milliseconds = integer.parseint(timearray[2]);     }     else if(timearray.length == 1){         this.minutes =  0;         this.seconds = integer.parseint(timearray[1]);         this.milliseconds =    integer.parseint(timearray[2]);                   }     else if(timearray.length == 0){         this.minutes = 0;         this.seconds = 0;         this.milliseconds = integer.parseint(timearray[2]);                  }     else{         this.minutes = 0;         this.seconds = 0;         this.milliseconds = 0;     } } 

p.s. junit test says "comparisonfailue: expected 0:00:000 20:10:008" when trying do:

s = new stopwatch("20:10:8"); assertequals(s.tostring(),"20:10:008"); 

as mentioned in other answers, lengths off 1 each each, index's using in if block off; eg. if length 1, index available 0, if length 2, index's available 0 , 1.

thus constructor looks like:

class stopwatch {     int minutes;     int seconds;     int milliseconds;      public stopwatch(string starttime) {         string[] timearray = starttime.split(":");          if (timearray.length == 3) {             this.minutes = integer.parseint(timearray[0]);             this.seconds = integer.parseint(timearray[1]);             this.milliseconds = integer.parseint(timearray[2]);         } else if (timearray.length == 2) {             this.minutes = 0;             this.seconds = integer.parseint(timearray[0]);             this.milliseconds = integer.parseint(timearray[1]);         } else if (timearray.length == 1) {             this.minutes = 0;             this.seconds = 0;             this.milliseconds = integer.parseint(timearray[0]);         } else {             this.minutes = 0;             this.seconds = 0;             this.milliseconds = 0;         }     } } 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -