1

I have an array with 2 data fields in each element:

String[] myArr = {"Bob    Marley", "Barbara    Newton", "John    Smith"};

The first and last names and separated by a tab ("\t"). How would I go about splitting them into two arrays, for example:

String[] firstName = {"Bob", "Barbara", "John"};
String[] lastName = {"Marley", "Newton", "Smith"};

I initially tried split("\t") but that didn't work, and I've tried looking up for similar questions here to no avail.

One thing to note, I am not using ArrayList.

Any help would be immensely appreciated. Thank you in advance.

Code snippet:

public static String[] sortNames(String[] info) {

    String[] firstName = new String[info.length];
    String[] lastName = new String[info.length];

    for(int i = 0; i < info.length; i++) {
        firstName[i] = info[i].split("\t");
    }

    return firstName;
}
Shaun Y
  • 41
  • 1
  • 8

4 Answers4

2

firstName[i] = info[i].split("\t"); is assign an array to an element,it will cause compile failure.

public static String[] sortNames(String[] info) {

    String[] firstName = new String[info.length];
    String[] lastName = new String[info.length];

    for(int i = 0; i < info.length; i++) {
        String[] infos = info[i].split("\t");
        firstName[i] = infos[0];
        lastName[i] = infos[1];
    }

    return firstName;//also,you might need to change your return type to String[][] so that both array can be returned
}
flyingfox
  • 13,414
  • 3
  • 24
  • 39
0

You could have your sortNames method return a two-dimensional array:

import java.util.Arrays;

class Main {
  public static void main(String[] args) {
    String[] myArr = {"Bob    Marley", "Barbara    Newton", "John    Smith"};
    String[][] names = sortNames(myArr);
    String[] firstNames = names[0];
    String[] lastNames = names[1];
    System.out.println("names: " + Arrays.deepToString(names));
    System.out.println("firstNames: " + Arrays.toString(firstNames));
    System.out.println("lastNames: " + Arrays.toString(lastNames));
  }

  public static String[][] sortNames(String[] info) {
    int infoArrLength = info.length;
    String[][] names = new String[2][infoArrLength];
    for(int i = 0; i < infoArrLength; i++) {
      String[] infos = info[i].split("\\s+");
      names[0][i] = infos[0];
      names[1][i] = infos[1];
    }
    return names;
  } 
}

Output:

names: [[Bob, Barbara, John], [Marley, Newton, Smith]]
firstNames: [Bob, Barbara, John]
lastNames: [Marley, Newton, Smith]
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
0
String[] myArr = {"Bob    Marley", "Barbara    Newton", "John    Smith"};
    String[] firstName = new String[myArr.length];
    String[] lastName = new String[myArr.length];
    for (int i = 0; i < myArr.length; i++) {
        String[] splitString = myArr[i].split("\\s+");
        firstName[i] = splitString[0];
        lastName[i] = splitString[1];
    }
0

You can simply use java regx to perform the splitting.

\s => A whitespace character, short for [ \t\n\x0b\r\f]

private static void splitArray(String[] arr) {
    int len = arr.length;
    String[] firstNames = new String[len];
    String[] lastNames = new String[len];
    for (int i = 0; i < len; ++i) {
        String[] names = arr[i].split("\\s+");
        firstNames[i] = names[0];
        lastNames[i] = names[1];
    }
    System.out.println(Arrays.deepToString(firstNames));
    System.out.println(Arrays.deepToString(lastNames));
}

If I were you, in your case, I'd prefer using javafx.util.Pair to bind the first and last name together as follows and in java 8 using stream will make it cleaner:

private static void splitArrayUsingStream(String[] arr) {
    Pair[] pairs = Arrays.stream(arr).map(s -> {
        String[] names = s.split("\\s+");
        return new Pair<>(names[0], names[1]);
    }).collect(Collectors.toList()).toArray(new Pair[arr.length]);
    System.out.println(Arrays.deepToString(pairs));
}

These methods will give us output as:

[Bob, Barbara, John]
[Marley, Newton, Smith]
[Bob=Marley, Barbara=Newton, John=Smith]
Hearen
  • 7,420
  • 4
  • 53
  • 63