1

So, I'm receiving an array of String and I would like to split each element and save it into a new array and I have faced a lot of problems with that and came up with a really bad solution:

String[] timeSlots = { "13:00:00 - 14:00:00", "15:00:00 - 16:00:00","17:00:00 - 18:00:00" };
String[] t = new String[6];
String temp[] = new String[6];
int j = 1;
for (int i = 0; i < 3; i++) {
    temp = timeSlots[i].split("\\-");
    if(j == 1){
        t[0] = temp[0];
        t[1] = temp[1].trim();
    }
    else if(j == 2){
        t[2] = temp[0];
        t[3] = temp[1].trim();
    }
    else{
        t[4] = temp[0];
        t[5] = temp[1].trim();
    }
    j++;
}

As you can see I have to create an if-statement to save two elements, I know that it's a bad approach but that's all I was able to come with :(

Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
HSHERU
  • 13
  • 5
  • So what you need is to take an array A of strings like "1111-222", "333-444", "555-666" and store it into an array B of string "111", "222", "333"[...]? Does it need to be an array? can it be a List? The string of the array A are allways in that format? – Aimnox May 25 '16 at 08:02
  • @Aimnox yes that's what i want and yes it needs to be an array :( and yeah they will be in that format no matter what if it was wrong the program won't run – HSHERU May 25 '16 at 08:21
  • Does the second array need to be an array? Can it be a List? – Aimnox May 25 '16 at 08:22
  • @Aimnox sorry check new edited comment – HSHERU May 25 '16 at 08:23
  • In that case, @fabian solution seems to be all that you need – Aimnox May 25 '16 at 08:27

3 Answers3

2

You can calculate the index in the result array from the index in the input array:

String[] t = new String[2*timeSlots.length];

for (int i = 0; i < timeSlots.length; i++) {
    String[] temp = timeSlots[i].split("\\-");
    t[2*i] = temp[0].trim();
    t[2*i+1] = temp[1].trim();
}

Or use streams:

t = Arrays.stream(timeSlots).flatMap(slot -> Arrays.stream(slot.split("\\-")).map(String::trim)).toArray(String[]::new);

(this however trims both strings)

fabian
  • 80,457
  • 12
  • 86
  • 114
  • what if the timeSlots length changed so instead of 6 it became 4 or 8 or even more will that still solve the problem? – HSHERU May 25 '16 at 08:16
  • It will, as you can see its declaring the array t as the double of timeSlots. `String[] t = new String[2*timeSlots.length];` It will not work, however, if the strings on timeSlots contain more than one time. – Aimnox May 25 '16 at 08:24
0
@Test
public void splitTimeSlotsToArray() {
    String[] timeSlots = { "13:00:00 - 14:00:00", "15:00:00 - 16:00:00","17:00:00 - 18:00:00" };

    // We already know how many times there are, each range (or slot)
    // has two times specified in it. So it's the length of timeSlots times 2.
    String[] times = new String[timeSlots.length*2];

    for (int i = 0; i < timeSlots.length; i++) {
        String timeSlotParts[] = timeSlots[i].split(" - ");
        times[i*2] = timeSlotParts[0];
        times[i*2 + 1] = timeSlotParts[1];
    }

    assertEquals(Arrays.asList(
        "13:00:00", "14:00:00", "15:00:00", "16:00:00", "17:00:00", "18:00:00"
    ), Arrays.asList(times));
}

// This is a more preferable option in terms of readability and
// idiomatics in Java, however it also uses Java collections which you
// may not be using in your class
@Test
public void splitTimeSlotsToList() {
    String[] timeSlots = { "13:00:00 - 14:00:00", "15:00:00 - 16:00:00","17:00:00 - 18:00:00" };
    Collection<String> times = new ArrayList<>();

    // Go over each time slot
    for (String timeSlot : timeSlots) {
        // Go over each time in each time slot
        for (String time : timeSlot.split(" - ")) {
            // Add that time to the times collection
            times.add(time);
        }
    }
    // you can convert the Collection to an array too:
    // String[] timesArray = times.toArray(new String[timeStamps.size()]);

    assertEquals(Arrays.asList(
        "13:00:00", "14:00:00", "15:00:00", "16:00:00", "17:00:00", "18:00:00"
    ), times);
}
Ben Barkay
  • 5,473
  • 2
  • 20
  • 29
0

If the structure of your array is always the same you can first join the elements of your array to a string and split again after each hour. Example:

public static void main(String a[]){
    String[] timeSlots = { "13:00:00 - 14:00:00", "15:00:00 - 16:00:00","17:00:00 - 18:00:00" };
    String joined = String.join(" - ", timeSlots);// gives you a string like this "13:00:00 - 14:00:00 - 15:00:00 - 16:00:00 - 17:00:00 - 18:00:00"
    String [] newArray = joined.split(" - ");
    System.out.println(Arrays.toString(newArray));
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28