import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
class Time implements Comparable<Time>{
int hour;
int minute;
int second;
public Time(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
@Override
public String toString() {
return String.format("%02d:%02d:%02d", this.hour, this.minute, this.second);
}
@Override
public int compareTo(Time o) {
if(this.hour<o.hour){
return 1;
}
if(this.hour>o.hour){
return -1;
}
if(this.minute<o.minute){
return 1;
}
if(this.minute>o.minute){
return -1;
}
if(this.second<o.second){
return 1;
}
return 1;
}
}
public class Main {
static String parser(String str){
StringTokenizer st=new StringTokenizer(str);
Time[] time=new Time[st.countTokens()];
int i=0;
while (st.hasMoreTokens()) {
StringTokenizer st2=new StringTokenizer(st.nextToken(),":");
time[i]=new Time(Integer.valueOf(st2.nextToken()),
Integer.valueOf(st2.nextToken()),
Integer.valueOf(st2.nextToken()));
i++;
}
Arrays.sort(time);
StringBuilder sb=new StringBuilder();
for (int j = 0; j < time.length ; j++) {
if(j > 0){
sb.append(" ");
}
sb.append(time[j].toString());
}
sb.append("\n");
return sb.toString();
}
public static void main (String[] args) throws IOException {
File file = new File(args[0]);
BufferedReader buffer = new BufferedReader(new FileReader(file));
String line;
while ((line = buffer.readLine()) != null) {
line = line.trim();
System.out.print(parser(line));
}
}
}
Wow, this isn't easy. lol
ReplyDeletelol :D After a while you will see it is pretty easy :)
Delete