1

I am trying my hands on this questions:https://www.codeeval.com/browse/214/ and below is my code and ouput: CODE

public class TimeSort implements Comparable<TimeSort>{
   public int hour;
   public int minutes;
   public int seconds;

   public TimeSort(int hour, int minutes, int seconds) {
      this.hour = hour;
      this.minutes = minutes;
      this.seconds = seconds;
  }

  public TimeSort(String str){
    /*DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    final String x = str;
    try {
        Date time = sdf.parse(x);

        //Time time = new Time(new SimpleDateFormat("HH:mm:ss").parse(x).getTime());
        this.hour = time.getHours();
        this.minutes = time.getMinutes();
        this.seconds = time.getSeconds();
    } catch (ParseException e) {
        e.printStackTrace();
    }*/

    String[] parts = str.split(":");
    int hour = Integer.parseInt(parts[0]);
    int minutes = Integer.parseInt(parts[1]);
    int seconds = Integer.parseInt(parts[2]);

    this.hour = hour;
    this.minutes = minutes;
    this.seconds = seconds;
}

public int getHour() {
     return hour;
 } 

public void setHour(int hour) {
    this.hour = hour;
}

public int getMinutes() {
    return minutes;
}

public void setMinutes(int minutes) {
    this.minutes = minutes;
}

public int getSeconds() {
    return seconds;
}

public void setSeconds(int seconds) {
    this.seconds = seconds;
}

@Override
public String toString() {
    if(this.getHour() < 10){
        return "0"+this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
    }

    return this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}

public static void main(String[] args){

    List<String> inputs = new ArrayList<>();
    inputs.add("02:26:31 14:44:45 09:53:27");
    inputs.add("05:33:44 21:25:41");
    inputs.add("02:26:31 14:44:45 09:53:27 02:26:31 01:44:45 19:53:27");

    for (String input : inputs){
        sortTimes(input);
    }

}

@Override
public int compareTo(TimeSort timeSort) {
    if(this.getHour() > timeSort.getHour()){
        return -1;
    }
    else if(this.getHour() < timeSort.getHour()){
        return 1;
    }
    else if(this.getHour() == timeSort.getHour()) {
        if(this.getMinutes() > timeSort.getMinutes()){
            return -1;
        }
        else if(this.getMinutes() < timeSort.getMinutes()){
            return 1;
        }
    }
    else if(this.getSeconds() > timeSort.getSeconds()){
        return 1;
    }

    return 0;
}

public static void sortTimes(String str){
    List<TimeSort> list = new ArrayList<>();
    String[] times = str.split(" ");

    for (String time : times){
        list.add(new TimeSort(time));
    }

    System.out.print("Before Sorting: ");
    for (TimeSort t : list){
        System.out.print(t + " ");
    }
    System.out.println();
    Collections.sort(list);
    System.out.print("After Sorting: ");
    for (TimeSort t : list){
        System.out.print(t + " ");
    }
    System.out.println();
    System.out.println("==============================================================");
 }
}

OUTPUT

Before Sorting: 02:31:31 14:45:45 09:27:27 
After Sorting: 14:45:45 09:27:27 02:31:31 
==============================================================
Before Sorting: 05:44:44 21:41:41 
After Sorting: 21:41:41 05:44:44 
==============================================================
Before Sorting: 02:31:31 14:45:45 09:27:27 02:31:31 01:45:45 19:27:27 
After Sorting: 19:27:27 14:45:45 09:27:27 02:31:31 02:31:31 01:45:45 
==============================================================

The weird thing I am seeing is the times do not print correctly. For example 02:26:31 is print as 02:31:31. And it's the same even if I tried to parse the string time(commented part of the code)

Any help is appreciated. Thanks

3
  • Your are parsing dates by hand. That is so error-prone that it's not really us looking at the code any further. Use library code to parse, preferably from JSR-310. Commented Sep 25, 2015 at 14:47
  • 1
    @BoristheSpider it looks like this is a coding challenge so libraries are probably discouraged. Commented Sep 25, 2015 at 14:51
  • @mdnghtblue I am yet to meet a coding challenge where the use of standard JDK functionality is dis-couraged. But if that is the case, the the OP needs to make that clear in the question. Commented Sep 25, 2015 at 14:55

3 Answers 3

3

Your toString() method has a bug:

@Override
public String toString() {
    if(this.getHour() < 10){
        return "0"+this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
    }

    return this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}

Note that the 2nd field is getSeconds() instead of getMinutes().

Sign up to request clarification or add additional context in comments.

2 Comments

Yes that's the problem..thanks you. Gave you an up vote!
You were quicker ;-) +1
1

Your toString()-method does not print the minutes!

Comments

0

The other Answers found the bug in your code.

java.time

FYI, all of that code in unnecessary. Java 8 and later has the java.time framework built-in. Amongst its classes is LocalTime for representing a time-of-day-only value without a date and without a time zone. This class already knows how to compare and sort its values and how to parse/print its values. Printing follows standard ISO 8601 formats by default.

List<LocalTime> times = new ArrayList<>();
times.add( LocalTime.parse( "02:26:31" );
times.add( LocalTime.parse( "14:44:45" );
times.add( LocalTime.parse( "09:53:27" );
Collections.sort( times );

Dump to console.

System.out.println( times );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.