Question

We are asked to build a constructor for a Stopwatch that takes a string in the format of "##:##:###" and updates minutes, seconds and milliseconds (private instance variables) accordingly. For example, "1:21:300" indicates 1 minute 21 seconds 300 milliseconds.

So I am trying to use string.split() paired with parseInt to update values. However, the program will not compile. My constructor has the correct syntax according to eclipse, but there is something wrong with what I am doing. I have never actually used split nor parseInt, so I could be 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 but was 20:10:008" when trying to do:

s = new StopWatch("20:10:8");
assertEquals(s.toString(),"20:10:008");
Was it helpful?

Solution

As mentioned in other answers, the lengths are off by 1 each each, but the index's you are using in if block are also off; eg. if the length is 1, the only index available is 0, if the length is 2, the index's available are 0 and 1.

Thus you get a constructor that 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;
        }
    }
}

OTHER TIPS

Replace your toString() method with:

public String toString() {
    String paddedMinutes = String.format("%02d", this.minutes);
    String paddedSeconds = String.format("%02d", this.seconds);
    String paddedMilliseconds = String.format("%03d", this.milliseconds);
    return paddedMinutes + ":" + paddedSeconds + ":" + paddedMilliseconds;
}

Though Java arrays are zero-based, their lengths simply count the number of elements.

So, {1,2,3}.length will return 3.

As your code is written now you will be getting ArrayOutOfBounds exceptions left and right.

if(timeArray.length == 2){

should be:

if(timeArray.length == 3){

and so on.

20:10:8 split by : will give you a length of 3 ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top