0

I am doing some train application. In the application I am maintaining train time. From my database I can able to fetch times as String array list like this

   train_schedule_time------>[8.2, 13.55, 0.45]

Now I want to segregate my array by hour with corresponding minutes like this ...

enter image description here

I am using StringTokenizer and split into hour array and minute array. But i can't able to group my hour with multiple minutes and I have to show in list view. How can i do this? Can anybody help me? Thank in advance

AstroCB
  • 12,337
  • 20
  • 57
  • 73
malavika
  • 1,331
  • 4
  • 21
  • 54
  • Where is your tried code >? – Suresh Atta Oct 10 '13 at 06:06
  • I'm not sure what you are having trouble with, is it the list view? If your stringtokenizer is working correctly to produce the hour and minute array, can you post those, along with what you expect to be able to do with that/what you can't do? – Wayne Uroda Oct 10 '13 at 06:06

1 Answers1

2

You can split your String. For example:

String train_schedule_tim = "8.2, 13.55, 0.45";

String[] hours = train_schedule_tim.split(", ");

String hour1 = hours[0].split(".")[0];
String mins1 = hours[0].split(".")[1];

String hour2 = hours[1].split(".")[0];
String mins2 = hours[1].split(".")[1];

If you want to keep one hoy with several minutes, you can do something like this (the use of Integer or String is what you prefer):

Map<Integer, List<Integer>> hours = new HashMap<Integer, List<Integer>>();

List<Integer> minutes = new ArrayList<Integer>();
minutes.add(15);
minutes.add(30);
minutes.add(45);

hours.put(8, minutes);

Then, you can do:

for (Integer h : hours.keySet()) {
    List<Integer> mins = hours.get(h);
}
angel_navarro
  • 1,757
  • 10
  • 11
  • Like this you get your hour and minutes. Is this your matter? Regards – angel_navarro Oct 10 '13 at 06:09
  • No.Thanks for your answer. But what my need is i have same hours with multiple minutes like this train_schedule_time------>[8.2, 8.55, 8.45]. If i have values like this means how can i group my hours with corresponding minutes? – malavika Oct 10 '13 at 06:38
  • @asha, like this can be more useful for you. Inside "for" bucle, `h` would be your hour, and `mins` the minutes associated with this hour ;-) – angel_navarro Oct 10 '13 at 06:57