Race results

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 975 Views - Last Post: 03 January 2018 - 12:08 PM Rate Topic: -----

#1 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Race results

Posted 01 January 2018 - 12:02 PM

Well i'm kind off confused,i'm making a race program i'm confused at the point when i need to enter the participants results.
How should i enter the results in the following format hh:mm:ss:ff ? as a String ?
and how can i possibly sort them in ascendant order ?
Is This A Good Question/Topic? 0
  • +

Replies To: Race results

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13954
  • View blog
  • Posts: 55,699
  • Joined: 12-June 08

Re: Race results

Posted 01 January 2018 - 12:05 PM

I would imagine showing the relevant time completed, some identification string, and placed in an ordered list would do. Heck you could make a small class to hold name and time and use an list collection to do the ordering of said custom class.
Was This Post Helpful? 0
  • +
  • -

#3 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Race results

Posted 01 January 2018 - 12:09 PM

can you pls show me an example bcs i didnt understand .
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13954
  • View blog
  • Posts: 55,699
  • Joined: 12-June 08

Re: Race results

Posted 01 January 2018 - 12:16 PM

Ahhh.. if you mean a class with two properties with a string and a time variable in it.. ah.. no. Any example would be essentially doing it. Why not try it out first.
Was This Post Helpful? 0
  • +
  • -

#5 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Race results

Posted 01 January 2018 - 12:23 PM

I'm kind of confused is there any other possible ways then a string to add time in the following format hh:mm:ss:ff ?
using Date you can only add hours seconds and minutes thats why i decided to use String in the first place .
but sorting those Strings is my problem.
to be more clear suppose i have an array of Strings named results
results[0]=20:15:10:40;
results[2]=10:15:20:25;
results[3]=23:20:25:30;
result[4]=23:20:25:35;

how to sort this array in ascending order .
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 780
  • View blog
  • Posts: 3,184
  • Joined: 13-June 14

Re: Race results

Posted 01 January 2018 - 12:33 PM

Is really what you want a Duration?
Was This Post Helpful? 1
  • +
  • -

#7 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Race results

Posted 01 January 2018 - 12:44 PM

can i use Collection.sort on an arrayList of durations ?
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 780
  • View blog
  • Posts: 3,184
  • Joined: 13-June 14

Re: Race results

Posted 01 January 2018 - 12:49 PM

Given that Duration implements the Comparable interface, yes. It would be a bit strange if you couldn't ask if one Duration was smaller than another, wouldn't it?

This post has been edited by ndc85430: 01 January 2018 - 12:49 PM

Was This Post Helpful? 1
  • +
  • -

#9 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1575
  • View blog
  • Posts: 3,542
  • Joined: 05-April 11

Re: Race results

Posted 01 January 2018 - 12:49 PM

You shouldn't be using the old Java Date class if you're using Java 8 or above.
In Java 8 there's a new date-time api which includes a Duration class.
long startNanoTime = System.nanoTime();
Thread.sleep(1000); // Race
long endNanoTime = System.nanoTime();

Duration raceDuration = Duration.ofNanos(endNanoTime - startNanoTime);

/*
 * Not working piece of code
 * String durationDisplayValue = String.format("%02d:%02d:%02d:%02d", raceDuration.toHours(), raceDuration.toMinutes(), raceDuration.toMillis(), raceDuration.toNanos());
 * System.out.println(durationDisplayValue);
*/


https://docs.oracle....e/Duration.html

Forget about the format example, that one is clearly wrong, sorry about that :D

This post has been edited by CasiOo: 01 January 2018 - 12:52 PM

Was This Post Helpful? 2
  • +
  • -

#10 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Race results

Posted 01 January 2018 - 12:56 PM

sounds like the best thing to use i'll try it and let you know if i have any problem.
Thank you very much for your help.
Was This Post Helpful? 0
  • +
  • -

#11 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 780
  • View blog
  • Posts: 3,184
  • Joined: 13-June 14

Re: Race results

Posted 01 January 2018 - 01:00 PM

If you need tutorial material on this stuff, have a look at the Java Tutorials. There's clearly a section on date and time and there should be something on the Comparable interface and why you need it for sorting, too, but I'm having difficulty finding it right now. It is left as an exercise for you to find relevant material and read it :).
Was This Post Helpful? 1
  • +
  • -

#12 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Race results

Posted 01 January 2018 - 01:01 PM

Let's see if i got this right.
What i'll have to do is get the text result, split it on :
take each part and make a duration from it,make a duration.
place all durations in an array and the use Collection.sort on this array
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3534
  • View blog
  • Posts: 16,017
  • Joined: 20-September 08

Re: Race results

Posted 02 January 2018 - 03:11 AM

    public static String hmsSToIsoFormat(String duration) {
        String[] a = duration.split(":");
        return String.format("PT%sH%sM%s.%sS", a[0], a[1], a[2], a[3]);
    }



You can use the above to turn one of your strings into a format that can be passed to Duration.parse
Was This Post Helpful? 1
  • +
  • -

#14 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Race results

Posted 02 January 2018 - 10:17 AM

well what i did is set the results as Strings, create a comparator then use sort.

ArrayList a=new ArrayList();
     a.add("10:10:10:10");
     a.add("20:10:15:12");
     a.add("24:06:55:10");
     a.add("20:14:10:11");
     a.add("10:05:18:16");

     Comparator c= new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                StringBuilder sb = new StringBuilder(o1.toString());
                sb.deleteCharAt(2);sb.deleteCharAt(4);sb.deleteCharAt(6);
                
                
              String s1=sb.toString();
               System.out.println(s1);
               sb=new StringBuilder(o2.toString());
                sb.deleteCharAt(2);sb.deleteCharAt(4);sb.deleteCharAt(6);
               String s2=sb.toString();
                System.out.println(s2);
  int cmp = Integer.parseInt(s1) > Integer.parseInt(s2) ? +1 : Integer.parseInt(s1) < Integer.parseInt(s2) ? -1 : 0;
         return cmp;
            }
        };
     a.sort(c);

Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3534
  • View blog
  • Posts: 16,017
  • Joined: 20-September 08

Re: Race results

Posted 02 January 2018 - 10:40 AM

Well,

a. if the atoms are always of the same width, they will sort as String without numerical manipulation
b. using Duration will give you more control and extensibility

An example of b. OK - new use case, what was the time difference between the fastest and the slowest. How are you going to do that?
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2